【问题标题】:Laravel 5 Eloquent AND instead of whereLaravel 5 Eloquent AND 而不是 where
【发布时间】:2019-08-14 15:08:44
【问题描述】:

我正在尝试使用如下所示的过滤器获取列表,但它无法按我想要的方式工作。 在whereIn('lists.status', $status) 中,它应该只返回状态为 1,3 或 4 的列表。但它目前返回所有没有特定状态的列表。

$status = [1, 3, 4];
$lists = List::query();

$lists->join("users", "users.user_id", "=", "lists.user_id")
      ->join('details', 'details.id', '=', 'lists.id')
      ->whereIn('lists.status', $status) <--- this is not working
      ->where('users.name', "LIKE", "john")
      ->orWhere('details.name', "LIKE", "john");

【问题讨论】:

    标签: sql laravel laravel-5 eloquent


    【解决方案1】:

    您正在将orWhere 添加到查询的末尾,这使得查询返回details.name 匹配john 的每一行,无论状态如何。

    尝试关注

    $status = [1, 3, 4];
    $lists = List::query();
    
    $lists->join("users", "users.user_id", "=", "lists.user_id")
        ->join('details', 'details.id', '=', 'lists.id')
        ->whereIn('lists.status', $status)
        ->where(function($query) {
            $query->where('users.name', "LIKE", "john")
                ->orWhere('details.name', "LIKE", "john");
        });
    

    【讨论】:

    • @ekclone 就像在普通查询中将最后两个 where 放在括号之间
    • 天哪,你是救命稻草,我已经尝试解决这个问题好几个小时了
    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2020-06-27
    • 2013-06-04
    • 2020-01-01
    相关资源
    最近更新 更多