The content of this article is about the model (code) of Laravel source code analysis. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I wish the apes a happy National Day in advance. Eat well, drink well and play well. I will watch you on TV.
According to the single responsibility development principle, during the development process of laravel, each table should establish a model for external services and calls. Similar to this
namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $table = 'users'; }
Laravel’s data operations are divided into two types
DB facade
Eloquent ORM
In addition to their own characteristics, basic data operations are completed through Illuminate\Database\Query\Builder
calling methods to complete the entire SQL. You can also use the Builder class as the base class for the entire SQL operation. This class covers the following operation methods (partially shown)
method |
---|
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)
|
DB::table('user')->get();
Illuminate\Foundation\Application -> registerCoreContainerAliases()
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], .... ) }
public function __call($method, $parameters) { return $this->connection()->$method(...$parameters); }
##Eloquent ORM
User::find(1)
public static function __callStatic($method, $parameters) { return (new static)->$method(...$parameters); }
. In the same way,
User::get()
is called through
public function __call($method, $parameters) { if (in_array($method, ['increment', 'decrement'])) { return $this->$method(...$parameters); } return $this->newQuery()->$method(...$parameters); }
. This method finally ends with
new Builder(). <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">public function newEloquentBuilder($query)
{
return new Builder($query);
}</pre><div class="contentsignin">Copy after login</div></div>
Finally we arrive at Illuminate \Database\Eloquent\Builder file, this class covers the basic operations of ORM, such as find, findOrFail, etc. If you use the get method in your code, sorry, not here, it will still forward your request to the Illuminate\Database\Query\Builder class through the __call method
$this->query->{$method}(...$parameters);
The entire data operation is completed. .
The above is the detailed content of Laravel source code analysis model (code). For more information, please follow other related articles on the PHP Chinese website!