亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Eloquent Model 的 create 方法在哪里?

Eloquent Model 的 create 方法在哪里?

PHP
Helenr 2021-06-11 18:10:21
每個模型都可以通過質量分配在 Laravel 中創建:$flight = App\Flight::create(['name' => 'Flight 10']);在 Laravel 5.6 中哪里可以找到這個方法的代碼?我查看了課程,Illuminate\Database\Eloquent\Model但找不到create方法。我還檢查了所有特征(從HasAttributes到GuardsAttributes),但我也沒有在create那里找到方法。由于該類model不擴展任何其他類,因此我對create隱藏該方法的位置感到有些困惑。
查看完整描述

3 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

Eloquent 模型使用魔術方法(__call、__callStatic)將調用傳遞給 Eloquent Builder 類。因此,Model::create() 實際上是將調用傳遞給 Builder::create() 方法。

但是,如果您調查該方法,它與調用基本相同:

$model = new Model($attributes);
$model->save();

通過直通的(查詢)構建器的這種混合允許您使用查詢方法,例如 Model::where()


查看完整回答
反對 回復 2021-06-13
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

你可以在這里在 github 中找到它


public function create(array $attributes = [])

{

    return tap($this->newModelInstance($attributes), function ($instance) {

        $instance->save();

    });

}

/**

 * 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);

    });

}


查看完整回答
反對 回復 2021-06-13
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

檢查以下文件

PATH_TO_PROJECT/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php


查看完整回答
反對 回復 2021-06-13
  • 3 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號