laravel雄辩的多搜索过滤器

pn9klfpd  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(364)

给出下面的查询,如何添加更多where/add(mysql)字段,最多4个字段?

$query = Profile::select('field1', field2', 'field3, 'field4', DB::raw('SUM(likes.like) AS total'))
        ->leftJoin('users', 'users.id', '=', 'profiles.user_id')
        ->leftJoin('likes', 'likes.profile_id', '=', 'profiles.user_id')
        ->whereNotNull('users.id')
        ->groupBy('users.id')
        ->orderBy('users.last_login_at', 'DESC')
        ->get();

我尝试添加如下内容,结果导致405个错误:

$query = Profile::select('field1', field2', 'field3, 'field4', DB::raw('SUM(likes.like) AS total'))
            ->leftJoin('users', 'users.id', '=', 'profiles.user_id')
            ->leftJoin('likes', 'likes.profile_id', '=', 'profiles.user_id')
            ->whereNotNull('users.id');

    if ($field1 != 'condition') {    
        $query->where('profiles.field1', $field1)
    }
            $query->groupBy('users.id')
            ->orderBy('users.last_login_at', 'DESC')
            ->get();
dsf9zpds

dsf9zpds1#

select语句中有错误。

$query = Profile::select('field1', 'field2', 'field3', 'field4', "DB::raw('SUM(likes.like) AS total')")
        ->leftJoin('users', 'users.id', '=', 'profiles.user_id')
        ->leftJoin('likes', 'likes.profile_id', '=', 'profiles.user_id')
        ->where('users.id', "!=", '');

if ($field1 != 'condition') {    
   $query = $query->where('profiles.field1', $field1)
}
        $query = $query->groupBy('users.id')
        ->orderBy('users.last_login_at', 'DESC')
        ->get();

将附加值赋给同一变量 $query

相关问题