Joshuyo
265
5
Laravel

how to filter intermediate table

Posted 1 year ago by Joshuyo

Tables:

articles
    id
    title
    name

tags
    id
    name    


article_tag
    article_id
    tag_id

Article model:

public function tags()
{
    return $this->belongsToMany('App\Tag','article_tag', 'article_id', 'tag_id')->withTimestamps();
}

Here is how I filter and get results based on title field in ArticleController:

...
$articles = Article::where('title', 'like', '%'.$key.'%')->with('tags')->get();
return response()->json(['result' => $articles]);
...

How can I filter and get results based on article_tag.tag_id? Please note that I need to return the same json structure!

Thanks

Please sign in or create an account to participate in this conversation.