I have a table named categories
and a table named courses
. These tables have one-to-many relation, it means each course belongs to one category and each category has many courses.
Now, I want to get five categories
(order is not important yet) and 4 courses
for each of these categories. I tried some of the solutions on stackoverflow and around the web but none of them worked! One of them (the below code) gets 4 courses for first item only!
$result = Category::with(['courses' => function ($query) {
$query->take(4);
})->take(5)->get();
I solved this problem with for loop, but I wonder if laravel has its solution for this. I'll be appreciated for your answers. :)