Answer the question
In order to leave comments, you need to log in
How to get related data in one request?
There is:
class YarnController extends Controller
{
public function show($manufacturer, $collection = null)
{
if (!$collection) {
$collections = Collection::whereHas('manufacturer', function($query) use ($manufacturer) {
$query->where('id', $manufacturer);
})->get();
} else {
$collections = Collection::whereHas('manufacturer', function($query) use ($manufacturer) {
$query->where('id', $manufacturer);
})->where('id', $collection)->get();
}
return view('yarn', compact('collections'));
}
}
@foreach ($collections as $collection)
<p>{{ $collection->manufacturer->name }} - {{ $collection->name }}</p>
@endforeach
select * from `collections` where exists (select * from `manufacturers` where `collections`.`manufacturer_id` = `manufacturers`.`id` and `id` = '2')
select * from `manufacturers` where `manufacturers`.`id` = 2 limit 1
select * from `manufacturers` where `manufacturers`.`id` = 2 limit 1
select * from `manufacturers` where `manufacturers`.`id` = 2 limit 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question