How to fix search and sort in eloquent relationship? #561
-
I want to be able to search and sort by eloquent relationships. I have a Transaction model that belongs to Partner. and when I search I get this error: according to documentation:
but it doesn't work in my case. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello, I think maybe this can help for you: |
Beta Was this translation helpful? Give feedback.
-
@gal01azabolcs thanks for your answer. I think your answer will help me. thanks. |
Beta Was this translation helpful? Give feedback.
Hello, I think maybe this can help for you:
->searchable(function ($builder, $term) {
return $builder->orWhereHas('restaurant',function($query) use($term){
$query->where('name', 'LIKE', '%' . $term . '%');
});
})
->sortable(function(Builder $query, $direction) {
return $query->orderBy(Restaurant::select('name')->whereColumn('products.restaurant_id', 'restaurants.id'), $direction);
}),