【问题标题】:Laravel 5 mysql query ->when conditionLaravel 5 mysql查询->何时条件
【发布时间】:2017-02-14 23:34:40
【问题描述】:

我的查询目前看起来像这样,它应该可以工作。

$status = 'active';

$data = DB::table('applicant')
->when($status, function($query) use ($status){
            return $query->where('Status', $status);})
->get();

但我收到此错误:

Call to undefined method Illuminate\Database\Query\Builder::when()

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    检查你的代码:闭包没有关闭,尝试如下写第5行:

    return $query->where('Status', $status);})
    

    【讨论】:

    • 对不起,我确实有这些,当我复制和粘贴时它没有复制。仍然抛出相同的错误,可能是版本问题,因为它适用于 laravel 5.2.39 ,上面的版本是 5.0.16 。
    • 我认为也是版本问题:when() 函数是在 5.2.34 中添加的
    • 你知道另一种方法吗?
    猜你喜欢
    • 2021-12-21
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多