Laravel framework source code analysis model principle and usage analysis


This paper introduces the principle and usage of model, which is the model of source code analysis of larravel framework. For your reference, the details are as follows:


I wish the ape people a happy National Day in advance. Eat well, drink well and play well. I will watch you on TV.

According to the principle of single responsibility development, each table should establish a model for external services and calls during the development process of laravel. It’s like this

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
 protected $table = 'users';


There are two types of data operations in laravel

  • DB facade
  • Eloquent ORM

In addition to their own characteristics, the basic data operation is throughIlluminate\Database\Query\Builder Call the method to complete the entire SQL. You can also help builder as the base class for the entire SQL operation. This class covers the following operation methods (shown in part)

public function select($columns = ['*'])
public function selectSub($query, $as)
public function selectRaw($expression, array $bindings = [])
public function fromSub($query, $as)
public function fromRaw($expression, $bindings = [])
public function addSelect($column)
public function distinct()
public function from($table)
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
public function joinWhere($table, $first, $operator, $second, $type = 'inner')
public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
public function leftJoin($table, $first, $operator = null, $second = null)
public function where($column, $operator = null, $value = null, $boolean = 'and')
public function orWhere($column, $operator = null, $value = null)
public function whereRaw($sql, $bindings = [], $boolean = 'and')
public function whereIn($column, $values, $boolean = 'and', $not = false)
public function orWhereIn($column, $values)

It can be seen that there are many methods in China’s laravel station or official documents are not reflected, so even if you want to master a framework, do not look at its source code is not good. This file is in your project directoryvendor/laravel/framework/src/Illuminate/Database/Query Next, you can check it by yourself.

DB facade

Normally, you might write an operation like this


This operation first points to the file through the facade of laravel, but it is not in the app.php Instead, it is loaded directly through the kernel

Illuminate\Foundation\Application -> registerCoreContainerAliases()

Is registered. Facade direct callIlluminate\Database\DatabaseManager Class.

public function registerCoreContainerAliases()
  foreach ([
   'encrypter'   => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class],
   'db'     => [\Illuminate\Database\DatabaseManager::class],
   'db.connection'  => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
   'events'    => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
   'files'    => [\Illuminate\Filesystem\Filesystem::class],

Illuminate\Database\DatabaseManager There is not too much code inside, mostly dealing with database links. When you useDB::table()Will pass the

public function __call($method, $parameters)
 return $this->connection()->$method(...$parameters);

Forward, callingIlluminate\Database\Connection , user processingtable() Method, which will be adopted latertable() Method directionIlluminate\Database\Query Class, we talked about this class at the beginning, but I won’t say much here. Then, we’ll splice all kinds of SQL, execute SQL and end the battle

Eloquent ORM

Eloquent ORM is similar to DB facade. First, each eloquent ORM needs to inherit the parent classIlluminate\Database\Eloquent\Model
You’ll probably write like this


This method does not exist in the parent class. It will pass the

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

To forward the request call. In the same way


Through the

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

To call, this method will eventually use thenew Builder() And in the end,

public function newEloquentBuilder($query)
 return new Builder($query);

At last we arrivedIlluminate\Database\Eloquent\Builder File, this class covers the basic operations of ORM, such as find, findorfail, and so on. If you use the get method in your code, I’m sorry, it doesn’t, it will still pass__ The call method forwards your request toIlluminate\Database\Query\Builder Class


This completes the entire data operation.

More interested readers can view the special topics of this website: introduction and advanced course of larravel framework, summary of excellent development framework of PHP, introduction course of PHP object-oriented programming, introduction course of PHP + MySQL database operation and summary of common database operation skills of PHP

I hope that this article will be helpful to the PHP program design based on larravel framework.

Recommended Today

MVC and Vue

MVC and Vue This article was written on July 27, 2020 The first question is: is Vue an MVC or an MVVM framework? Wikipedia tells us: MVVM is a variant of PM, and PM is a variant of MVC. So to a certain extent, whether Vue is MVC or MVVM or not, its ideological direction […]