How does eloquent model work behind the scenes?

Posted in Intermediate , Laravel 17 Feb 2019

I have always wondered how eloquent model works behind the scenes in Laravel . In this post, we will talk in details about what happens behind the scenes when an eloquent model is accessed. This post requires a basic understanding of eloquent model so I would recommend reading it before going any further in below post.

I already have an eloquent model named Post.php created and we will be using it throughout this post for examples.

Below flow charts explains the working of eloquent model. I took example of two eloquent functions viz. all() and find() to explain working of eloquent model.

Flow Chart of Eloquent model function calls

Flow Chart of Eloquent model function calls



Important function referred in above flow chart are explained below

Magic function

_callStatic is one of the magic functions in Model.php which is invoked when called function is not found in Model.php. This function creates new instance of Model.php and calls the requested function (i.e. find in our case)

public static function __callStatic($method, $parameters)
{
    return (new static)->$method(...$parameters);
}

Instantiation of Model.php

Below is the snippet of constructor of Model.php (please note the comments in below snippet)

public function __construct(array $attributes = [])
{
    $this->bootIfNotBooted(); //boot() function allows to set up any event bindings you might require on that model. For example, you might want to send an email out every time a User is created. boot() also boots all of the bootable traits on the model. Refer to https://www.archybold.com/blog/post/booting-eloquent-model-traits for details on it

    $this->initializeTraits();

    $this->syncOriginal();// refer to for difference between orginal  and attributes https://stackoverflow.com/questions/34898343/why-laravel-model-duplicates-set-of-data-and-how-if-possible-to-have-only-one/34950084

    $this->fill($attributes);
}

Eloquent model has not made connection with database yet so model instantiation primarily takes care of below items:

  1. Booting the model
  2. Event binding for models (if there are any defined in boot method of model or in model traits)
  3. syncOrginal() is called. If there are any defaults attributes defined in model they will be set in memory as originals
  4. fill() is called without attributes

Magic function

_call is another magic functions of Model.php which is invoked when the called function from _callStatic is not defined. This function is responsible for forwarding the call to Eloquent builder

public function __call($method, $parameters)
{
    if (in_array($method, ['increment', 'decrement'])) {
        return $this->$method(...$parameters);
    }

    return $this->forwardCallTo($this->newQuery(), $method, $parameters);
}

Model hydration

Adding data to model is called model hydration. In case of find() and all(), the hydration is taken care by get() function of Eloquent Builder class.