Laravel 操作 Model

邹驰
2023-12-01

1、save()

$bool = $model->save();
返回 true

2、create()

       单条或者批量,维护时间戳

$obj = $model->create($data);
返回 model

3、insert()

       单条或者批量,不维护时间戳

$bool = $model->insert($data);
返回 bool

4、increments() / decrements()

$one = $model->increments('column');
返回 1

5、firstOrCreate()

$obj = $model->firstOrCreate(['id' => 1], ['value', 2]);
返回 1

6、updateOrCreate()

$obj = $model->updateOrCreate(['id' => 1], ['value', 2]);
返回 model

7、updateOrInsert()

$bool = $model->updateOrInsert(['id' => 1], ['value', 2]);
返回 bool

 

 

 

 

二、appends 中字段 会附加到 select 字段中,显示多余字段

$modelList = Model::get($visibleKey)
    ->each(function ($row) use ($visibleKey) {
        $row->setVisible($visibleKey);
    })
    ->toArray();

 

 

 

 

 

 

 类似资料: