spatie / laravel-schemaless-attributes

Add schemaless attributes to Eloquent models
https://spatie.be/en/opensource
MIT License
1.01k stars 58 forks source link

Chaining multiple scopeWithExtraAttributes #105

Closed chatisk closed 1 year ago

chatisk commented 1 year ago

Greetings everyone ,i have create a #104 relevant thread in discussion tab as well, I am not sure if it is ok to create an issue as well, remove it if it violates any rules.

So , i have a case where in a foreach

foreach ($allModelAttributes as $attribute) { $modelAttributeQuery->withExtraAttributes($attribute, 'LIKE', '%' . $this->search . '%'); }

I have to chain multiple withExtraAttributes method to search for a value in several schemaless attributes. Result of chaining is a query like Select * from Model where extra_attributes.name LIKE value AND ... , which results in a query that search for a value that exists in each schemaless attribute.

yes i could be doing a separate query for each attribute but this has significant performance impact on my system. My goal was to chain these withExtraAttributes and do a get() in the end.

All this come down to a method called modelScope() that builds the query, but always appends
$builder->where('name' , like , 'value') which is not my goal when i am trying to search through each schemaless attribute.

If i change the where to orWhere it achieves my goal.

If it exists, perhaps i am missing smth, but haven't seen antyhing like this in the package's code.

Thanks in advance for your time and happy to answer any question or even contribute if need be.

freekmurze commented 1 year ago

Duplicate of discussion