Laravel source code analysis model (code)

01-28-2024

What this article brings to you is the model (code) of Laravel source code analysis, which has certain reference value. Friends who need it can refer to it and hope to help you. Foreword I wish the apes a happy National Day in advance, eat well, drink well and have fun. I will watch you on TV.

According to the principle of single responsibility development, in the development process of laravel, each table should establish a model external service and call. Something like this

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

There are two operations to parse Laravel's data.

DB facade

Eloquent ORM

In addition to their own characteristics, the basic data operation is to complete the whole SQL by calling the method through Illuminate \ Database \ Query \ Builder. You can also help the Builder class as the base class for the whole SQL operation. This class covers the following operation methods (partially shown)

Method public function select($columns =

Eloquent ORMEloquent ORM is similar to DB facade. First of all, each Eloquent ORM needs to inherit the parent class Illuminate \ Database \ Eloquent \ Model. You will probably write this way.

User::find(1)

This method does not exist in the parent class, it will pass through the.

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

To forward the request call. in like manner; in a similar way

User::get()

Is through

public function __call($method, $parameters) { if (in_array($method,

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us