there is not way that you get duplicated records
Post::join( 'tags', 'tags.post_id', '=', 'posts.id' )
->where( 'title', 'LIKE', '%'.$keyword.'%' )
->orWhere( 'tags.name', $keyword )
->paginate( 15 );
Hi arcollector,
Your solution won't work for me as the posts/tags relationship is a many to many relationship, and I don't have a post_id field in my tags table, instead of that I have a pivot table where the relation is set.
That's why now I'm trying this:
$posts = Post::where('title','LIKE','%'.$keyword.'%')->orWhereHas('tags', function($query)
{
$query->where('name', 'LIKE', '%'.$keyword.'%');
})->paginate(15);
But It's still not working, I get a Call to undefined method Illuminate\Database\Query\Builder::orWhereHas() error.
I'll keep trying, thanks for the help!
what about this ???
Post::join( 'posts_and_tags', 'posts_and_tags.post_id', '=', 'posts.id' )
->join( 'tags', 'tags.id', '=', 'posts_and_tags.tag_id' )
->where( 'post.title', 'LIKE', '%'.$keyword.'%' )
->orWhere( 'tags.name', $keyword )
->paginate( 15 );
I finally did as explained here: https://laracasts.com/forum/1085-search-through-relationship
Thanks anyway!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community