Hey guys,
I have a small understanding problem when it comes to using with(). When requesting data in the frontend, I fully understand it:
$users = User::with("role")->get()
This will result in a select into the users table as well as a select into the roles table using the ids of the selected users. Makes sense in the frontend.
But currently I am asking myself if this makes sense when e.g. used within a job class?
$users = User::with("role")->get();
foreach($users as $user) {
//do something with $user->role->name;
}
I am not sure if Laravel is using the with("role") here correctly and not just fetching the role data using one select for each user within the loop. In that case it would make more sense to use a join and write something like this:
$users = User::select("users.*","roles.name as role_name")->join("roles","users.role_id","=","roles.id")->get();
foreach($users as $user) {
//do something with $user->role_name;
}
This is a very simple example, I hope you can get what I mean. Just need some clarification on this problem.
Best,
Damian