Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JackJones's avatar

Is there a cleaner way to loop conditions on a query?

I keep having to add little loops to my eloquent queries, it's not the end of the world, it just adds a few extra lines where I might be missing a potential callback function somewhere that might make things a bit cleaner:

            $query = Place::where($place->type_1 . '_id', $place->id)
                ->whereType_1($child_type)
                ->limit(1);

           // Here is where I'm talking about
            foreach ($slice as $item) {
                $query = $query->whereNull($item . '_id');
            }

            $result = $query->get()->first();

Is there any looping functionality in Eloquent?

0 likes
2 replies
Sergiu17's avatar

What is $slice? and what are your trying to do here? What is $item . '_id'?

$query = $query->whereNull($item . '_id');
JackJones's avatar

Slice is a list of columns and item is a column

Please or to participate in this conversation.