I have a blog and I'm wanting to display all categories with posts within a dynamic change date start and date end.
I have the following eloquent query:
$Categories = Category::with('Posts')
->whereHas('Posts', function ($query) {
$query->where('created_at', '>=', $StartDate);
$query->where('created_at', '<=', $EndDate);
})
->get();
This will return only return categories with Posts
between the two dates.
I've found online to add doesntHave
.
$Categories = Category::with('Posts')
->doesntHave('Posts')
->orWhereHas('Posts', function ($query) {
$query->where('created_at', '>=', $StartDate);
$query->where('created_at', '<=', $EndDate);
})
->get();
This returns Categories
that have no posts
or posts
within the date range. However, if there is a category that has posts
but none of them are within the date range, they won't appear at all.
How can I show all categories
and join the posts
within the date range? If there are no posts within the range it should return as null.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2oqOu3N
via IFTTT
Aucun commentaire:
Enregistrer un commentaire