I have basically this database structure:
- columns: id
- posts: id
- column_post: id, column_id, post_id
I’m trying to do it:
If I do this, it will return me all columns with all posts (in my case, all columns have more than 10 posts at least)
Column::with('posts')->get();
This also works fine, it orders the relationship as intended
Column::with([
'posts' => function (Builder $query) {
$query->orderBy('created_at', 'desc'); // this works fine
}
]);
Here comes the issue, the take(4)
, it sometimes will return me 0 results, even tho there’s no column with 0 posts. Sometimes it returns 1 or 2 results. Why is this inconsistent behavior happening?
Column::with([
'posts' => function (Builder $query) {
$query->orderBy('created_at', 'desc')->limit(4); // this doesn't work as intended
}
]);