Expressive search / where by user attributes

I added a custom attribute to my model

public function getTouchedAttribute() { ... 

I would like to add this to the request

 hasMany()->where('touched', ...) 

but obviously this is not a column in the table.

What is the most elegant way to achieve this behavior?

+7
eloquent laravel
source share
1 answer

One option (and probably the best in terms of performance) is to mimic an attribute with raw SQL functions. (Can't help it, because I don't know what touched does)

Another way is to use filter in the resulting collection:

 $collection = Model::all(); $filtered = $collection->filter(function($model){ return $model->touched == true; }); 
+6
source share

All Articles