DEV Community

Discussion on: Making the advanced search query with Eloquent-Builder in Laravel

Collapse
 
farrukh_uet profile image
Farrukh • Edited

Hi,
First of all thanks for your easy and great solution.
I found many solutions for eloquent search filters, but this is discrete :).

I felt that relational object filters were not discussed.
So here is my code for eloquent relational search, hope it will also help some one.

For relational object follow these steps:

I have two tables Profiles and Leads.
Each Profile has some Leads against it.

This is my coding style, please change this according to your code ethics.

// @$Profile is the instance of collection in my Controller function

$Profiles = $this->Profiles;
$Profiles= $this->ApplyFilters($Request, $Profiles)->get();

// Apply Filters to relational models

private function ApplyFilrers($Request, $Profiles){

foreach(request()->all() as $Key=>$Value){
// if($Key == “Category”){
$Profiles = $Profiles->whereHas(‘Leads’, function($Profile) use($Key, $Value){
// I have a CategoryFilter in my EloquentFilters\Leads
$Profile = EloquentBuilder::to($Profile, [ $Key=>$Value]);
});
//}
}

return $Profiles;

}

Collapse
 
mohammadfouladgar profile image
Fouladgar.dev

Thanks you for your kind words ;)

It was an interesting example.

Thank you for sharing.

Collapse
 
farrukh_uet profile image
Farrukh

Thanks for your appreciation.

Thread Thread
 
farrukh_uet profile image
Farrukh

Hi,
Can we do sorting for eloquent relations?

Thread Thread
 
mohammadfouladgar profile image
Fouladgar.dev

Yes,you can.
EloquentBuilder return an instance of Builder:

return EloquentBuilder::to(Blog::class,$filters)->with('latestPost')->OrderByDesc('latestPost.created_at')->get();
Thread Thread
 
farrukh_uet profile image
Farrukh

Thanks a lot for your reply.

I did not try it yet. Hope it will work as I wish.

Regards

Thread Thread
 
farrukh_uet profile image
Farrukh

Hi,

I used laravel debugger, when I used Eloquent Builder it shows me a (lot) number of queries.

is it normal or there is some method to reduce that

Regards
Farrukh

Thread Thread
 
mohammadfouladgar profile image
Fouladgar.dev

Hi,

The EloquentBuilder reduces the complexity of the queries and conditions of the code.Number of queries is not related to it.

Good luck

Thread Thread
 
farrukh_uet profile image
Farrukh

Thanks a lot dear

Collapse
 
farrukh_uet profile image
Farrukh • Edited

Also if you want to implement a filter for a columns against multiple values.
For example following is the array you mentioned in example, but with gender filter as an array.

[
'age_more_than' => '25',
'gender' => ['male','female','custom']
'has_published_post' => 'true',
]

class GenderFilter extends Filter
{

public function apply(Builder $builder, $value): Builder
{

    if(is_array($value)){
        return $builder->whereIn('gender', $value);
    }

    return $builder->where('gender', '=', $value);
}

}

Thread Thread
 
mohammadfouladgar profile image
Fouladgar.dev

Nice.

Type of the value parameter is mixed and you can set it to any value based on your requirement.

Thanks Dear @Farrukh