samedi 6 octobre 2018

Laravel eloquent multiple search filters

Given the query below, how do you add more where/add (mysql) fields conditions, up to 4 fields?

$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();

I've tried adding like below, which results in 405 errors:

    $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();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire