Back

Hey Laravel Developers

Did you know that when adding where conditionals in the Query Builder, you can use dynamic method names?

For example, if you want to fetch an order matching a particular quantity, you can call a dynamic method like whereQuantity($quantity) instead of using where('quantity', $quantity).

It just reads so much better! What do you think?

image

4

921

Personally, I try to avoid using dynamic because due to the large amount of processed information in general, it can put a heavy load on the CPU, so the query will be processed much slower.

So you have to choose between beauty and performance. In this case I choose the second one.

2

241

If we take a specific code example from the topics, I would prefer to write it like this:

$order = DB::table('orders')
->where(compact('quantity'))
->first();

And it is even better to use the Order model instead of direct reference to the table at least because in case of anything you will have to search through the code for all references to this table instead of Ctrl+Click on the class name.

$order = Order::query()
->where(compact('quantity'))
->first();

1

143

Using Model has always been a better option.

92