mercredi 23 novembre 2016

Laravel - Query results where there is a matching pivot table row

I have 3 tables: links, categories & link_category

I am filtering the links by category. I used to have a category field in the links table so could simply do a where in my query but now I need to check if there is a pivot row.

My links query is currently:

$links = Link::orderBy('created_at', 'desc')
    ->paginate(14);

Which returns all links fine.

However, I am now on e.g. /categories/categoryname.

In my controller I get the ID of the given category name from the categories table and need to modify my $links query so that it only returns results where there is a row in the link_category table that matches.

The link_category table has link_id and category_id.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire