K
K
Konstantin2018-12-11 03:20:01
Laravel
Konstantin, 2018-12-11 03:20:01

Is with evil in Laravel?

Is it evil to use with(<modelName>)in Laravel to join tables and fetch?
For example, this call will create 3 separate SELECT queries for each connection (debugged):

ProductModel::with(['ProductPrice', 'ProductDiscount', 'ProductUser'])...

Isn't it easier and most importantly more productive to use one query with two JOINs?
Or I don't understand how with() works .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Barmunk, 2018-12-11
@Junart1

User::with('comments')->get()
This:

select * from `users`
select * from `comments` where `comments`.`user_id` in (1, 2, 3, 4, 5)

in in some databases has limits, so when there are more than 2000 users, problems and crutches will begin . Therefore, with a large selection, it is better to use join, it will be faster and more logical

A
Alexander Filippenko, 2018-12-11
@alexfilus

JOINs are not always faster. Although in the general case they are recommended instead of several queries or subqueries.
I think the reason is that tables for models can belong to different bases, and it will still work.

K
Kirill Nesmeyanov, 2018-12-11
@SerafimArts

And now let's check how this join will work if we add LIMIT 1 to the end, for example;)
And, as it were, 3 requests are a penny.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question