Laravel查询构建不起作用,为什么?

时间:2015-07-07 12:19:18

标签: php laravel laravel-4

有人可以解释一下这个查询无效的原因吗?我正在尝试创建一个查询 - 客户姓名,电子邮件或电话符合必要的标准。但是,它似乎仅适用于一个标准(名称或电子邮件或电话),而不是几个标准。这样,我没有得到所需的结果,我留下了未过滤的列表。

使用 Laravel 4.1

$q = new Contract;
$q = $q->with(array('lsct' => function($q) {
    return $q->select(array('id', 'code'));
}, 'client' => function($q) {
    return $q->select(array('id', 'name', 'phone', 'email'));
}));

if(Input::has('search')) {
    $criterion = Input::get('search');
    $q = $q->where(function($q) use ($criterion) {
        $q->where('uid', 'like', '%' . $criterion . '%')->orWhere('car_number', 'like', '%' . $criterion . '%');
    })->orWhereHas('lsct', function($q) use($criterion) {
        $q->where('code', 'like', '%' . $criterion . '%');
    })->orWhereHas('client', function($q) use($criterion) {
        $q->where('name', 'like', '%' . $criterion . '%');
        // When i try to add condition below, this 'orWhereHas' not working at all
        // ->orWhere('phone', 'like', '%' . $criterion . '%')
        //->orWhere('latakko_id', 'like', '%' . $criterion . '%');
    });
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过安装调试栏以准确显示您的应用程序正在查询的内容?

Here is the link for Laravel 4

如果你能真正看到被查询的内容,这可能会给你一些线索。