Support the ongoing development of →
Database Eloquent
Last updated 2 years ago.

This can be cleaned up a lot...


All those wheres could be thrown into query scopes so you can create more readable code. To access inside the categories relationship, you need the parens with creates a query builder. I don't have your data to test, but this should help you.


Thank you rocketpastsix for your advice, I will read more about those wheres and take it in count.

I received solution from users in laracast forum, so I share here, because it may helps somebody else.

Correct query in eloquent way is:

$banners = Banner::where('visible', 1)
    ->where('visible', 1)
    ->where('up_at','<=', $today)
    ->where('down_at','>=', $today)->get();
    ->whereHas('categories', function($query) use ($categoryId)  {
        $query->where('id', $categoryId);

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.