Model类继承其批量赋值的create方法在哪里?

时间:2018-03-14 17:39:35

标签: php laravel

我知道这可能是一个非常初学者的问题,而且最明确的原因是我缺乏PHP经验。所以不要恨我。

过去几周我一直在学习Laravel。在他们的大众作业文档中,他们讨论了create方法。按照他们的例子 -

$flight = App\Flight::create(['name' => 'Flight 10']);

然而,尽管我尝试在API文档或源代码中进行搜索,但我似乎无法找到它引用的方法,至少在Model类中没有。我找到的唯一合理的东西是来自Eloquent中的Builder类的方法。

我不希望任何人为我搜索文档。我更感兴趣的是知道找到特定方法的继承链并记录或转储它的最佳实践。

2 个答案:

答案 0 :(得分:1)

create()方法位于Illuminate\Database\Eloquent\Builder

https://github.com/laravel/framework/blob/5.6/src/Illuminate/Database/Eloquent/Builder.php#L754

您可以通过查看Model::__callStatic()__call()找到它 这些是神奇的方法(http://php.net/manual/en/language.oop5.magic.php)。

答案 1 :(得分:0)

它来自Builder类。你可以在这里看到代码:

/**
 * Save a new model and return the instance. Allow mass-assignment.
 *
 * @param  array  $attributes
 * @return \Illuminate\Database\Eloquent\Model|$this
 */
public function forceCreate(array $attributes)
{
    return $this->model->unguarded(function () use ($attributes) {
        return $this->newModelInstance()->create($attributes);
    });
}

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Builder.php#L754