Laravel模型源码中model类是如何编写的?

2026-05-07 04:011阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计233个文字,预计阅读时间需要1分钟。

Laravel模型源码中model类是如何编写的?

Eloquent ORM与DB facade类似,每个Eloquent ORM都需要继承父类 Illuminate\Database\Eloquent\Model。

User::find(1)

父类是不存在这个方法的,它会通过

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

去转发请求调用。同理

User::get()

则是通过

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

去调用,这个方法最终以 new Builder() 而告终,

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

最后我们到了  Illuminate\Database\Eloquent\Builder  文件下,这个类中涵盖了ORM的基本操作,例如find , findOrFail 等等。

阅读全文

本文共计233个文字,预计阅读时间需要1分钟。

Laravel模型源码中model类是如何编写的?

Eloquent ORM与DB facade类似,每个Eloquent ORM都需要继承父类 Illuminate\Database\Eloquent\Model。

User::find(1)

父类是不存在这个方法的,它会通过

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

去转发请求调用。同理

User::get()

则是通过

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

去调用,这个方法最终以 new Builder() 而告终,

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

最后我们到了  Illuminate\Database\Eloquent\Builder  文件下,这个类中涵盖了ORM的基本操作,例如find , findOrFail 等等。

阅读全文