I have a many to many relationship with a pivot table.
goods
id | title | ...
tags
id | title | ...
good_tag
id | good_id | tag_id | ...
How correctly retrieve all goods and their tags in laravel?
Thanks
I have a many to many relationship with a pivot table.
goods
id | title | ...
tags
id | title | ...
good_tag
id | good_id | tag_id | ...
How correctly retrieve all goods and their tags in laravel?
Thanks
With Eager Loading
$goods = Good::with('tags')->get();
foreach ($goods as $good) {
// each goods
echo $good->title;
foreach ($good->tags as $tag) {
// each tag for that goods
echo $tag->title;
}
}
Each returned Good model will have its collection of Tags attached.