try
$posts = Post::whereRaw( "MATCH(title,body) AGAINST(? IN BOOLEAN MODE)", [$q] )->with('categories')->get();
in where use post_categories.id
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have defined many to many relationship between my Post and Category models. I need to perform FULL TEXT search on column title and body in posts table. The search may also have supplied ids of category when submitted from search form.
How can i perform this search? My current codes
//Post.php
public function categories()
{
return $this->belongsToMany('App\Category', 'post_categories');
}
//Category.php
public function posts()
{
return $this->belongsToMany('App\Post', 'post_categories');
}
// my current query to search
$posts = Post::whereRaw( "MATCH(title,body) AGAINST(? IN BOOLEAN MODE)", [$q] )->get();
How can i modify the current query to have the relationship ? I mean how I can add where clause to contain only those row which belongs to supplied categories ids?
Any other suggestion related to improving my logic or code?
Thank you in advance
Please or to participate in this conversation.