teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.09k stars 142 forks source link

Mapping empty collection should also use newCollection() from model #338

Closed it4need closed 2 years ago

it4need commented 2 years ago

Problem

\App\Models\Model::search($search_term)->get()->onlyViewableInFrontend();

throws an exception

Method Illuminate\Database\Eloquent\Collection::onlyViewableInFrontend does not exist.

iff (if, and only if), the result of

\App\Models\Model::search($search_term)->get()

returns an empty collection.

Potential Solution

To make sure, that methods defined in the custom Collections class (models newCollection() method) are also valid for empty collections, you should consider to always return the custom type of the collection.

return $model->newCollection([]);

instead of https://github.com/teamtnt/laravel-scout-tntsearch-driver/blob/b98729b0c7179218c9a5e1445922a9313d45c487/src/Engines/TNTSearchEngine.php#L200