1
I remember in the old days "discovering" that in a query using Eloquent, if I used the with
Laravel made a inner join
.
Today by chance I went to check the queries of a project and...
[2014-11-20 23:21:16] sql.INFO: select * from `ocurrences` where `ocurrences`.`deleted_at` is null order by RAND() limit 4 {"bindings":[],"time":3.58,"name":"mysql"} []
[2014-11-20 23:21:16] sql.INFO: select * from `users` where `users`.`id` in ('7') {"bindings":["7"],"time":0.49,"name":"mysql"} []
[2014-11-20 23:21:16] sql.INFO: select * from `users` where `users`.`id` = '7' limit 1 {"bindings":["7"],"time":0.51,"name":"mysql"} []
[2014-11-20 23:21:16] sql.INFO: select * from `tags` limit 5 {"bindings":[],"time":0.41,"name":"mysql"} []
In this case, I’m query this way:
/**
* Get random ocurrences for home
* @return mixed
*/
public static function randomForHome()
{
return static::with('user')
->orderByRaw('RAND()')
->limit(4)
->get();
}
What’s wrong and/or how I do it with the Eloquent joins?
with simply includes the registered method for relationship in your model. In this case, a select for each table.
– Wallace Maxters