protonemedia / laravel-cross-eloquent-search

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package
MIT License
1.1k stars 80 forks source link

Models with nullable relationships do not appear in search results #37

Closed ronvdberkmortel closed 2 years ago

ronvdberkmortel commented 3 years ago

Perhaps the package already contains a solution to this problem, but I was not able to find it. Say you have a model (e.g. Post) with a relationship to another model (e.g. Author). In this example use case, the relationship field can be null, meaning that a post has no author. First, a simple search query:

Search::add(Post::class, ['title'])->get();

The query above works fine, showing all posts (including posts with no author) and allowing the user to search using the post title column. If we want to add the ability to search on the author's name, we have to add the related model column to the query using the dot notation.

Search::add(Post::class, ['title', 'author.name'])->get();

This query works, but appears to use an inner join behind the scenes. As a result, all posts without an author are excluded from the results. The desired behavior would be to show all posts (when no search query is used) and all matching posts (when a search query is used), including posts that lack an author but where the title is a match to the search query.

pascalbaljet commented 3 years ago

Thanks for noticing! I understand the issue and will take a look at it soon.

ronvdberkmortel commented 3 years ago

Have you, by any chance, had time to look at this issue @pascalbaljet? It would be great addition to the package.

pascalbaljet commented 2 years ago

Fixed in v2.6.1.