Laravel красноречивый множественные поисковые фильтры - PullRequest
0 голосов
/ 07 октября 2018

Учитывая приведенный ниже запрос, как вы можете добавить больше условий где / 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();

1 Ответ

0 голосов
/ 07 октября 2018

В вашем операторе выбора есть ошибки.

 $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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...