E
E
Ernest Faizullin2018-09-22 06:39:18
Laravel
Ernest Faizullin, 2018-09-22 06:39:18

Laravel. What is "eager loading" in a nutshell?

That's the whole question. Hi all!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Inclusive, 2018-09-22
@Inclusive

In the documentation, everything is simply painted.
There is a model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

You have selected users: If you do this:
foreach($users as $user){
   $phone = $user->phone;
}

That at each request to the relation - there will be a separate request.
Instead, you can do either:
or after receiving: In the first case, if there are 10 records, 11 requests will occur with users, in the second and third, only 2 requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question