Support the ongoing development of Laravel.io →
Database Eloquent Queues
Last updated 1 year ago.
0

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 );
Last updated 1 year ago.
0

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!

Last updated 1 year ago.
0

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 );
Last updated 1 year ago.
0

I finally did as explained here: https://laracasts.com/forum/1085-search-through-relationship

Thanks anyway!

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ulisessp ulisessp Joined 5 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.