(['delayed' => 1]); update 方法接受一个字段为键、更新数据为值的数组 4.create 自动维护 created_at 和 updated_at两个字段 除了 save 和 saveMany
还可以通过 saveMany 方法一次插入多条关联记录,前提是为关联模型配置了批量赋值,比如我们为 Comment 模型类配置白名单 $fillable 属性如下(你也可以不配置批量赋值,但是需要多次实例化并逐个设置评论模型属性值 这样我们就可以批量插入文章评论数据了: $post = Post::findOrFail(1); $faker = \Faker\Factory::create(); $post->comments()->saveMany $faker->paragraph, 'user_id' => mt_rand(1, 15)]) ]); 此外,我们还可以通过 create/createMany 方法来插入关联数据,与 save/saveMany )->save( new Tag(['name' => $faker->word]), ['user_id' => 1] ); // 插入多条记录 $post->tags()->saveMany Tag(['name' => $faker->unique()->word]) ]); // 如果插入多条记录需要传递中间表额外字段值(通过键值关联对应记录与额外字段) $post->tags()->saveMany
这是写入一条,你也可以写入多条: $contact->phoneNumbers()->saveMany([PhoneNumber::find(1),PhoneNumber::find(2)]); 或者知道关联模型字段名的
在调用save方法向Phone模型插入值 这里Eloquent自动在phones表中添加了uid字段,并插入正确的值 使用saveMany添加多个值 $user = \App\Muser::find (1); $user->phone()->saveMany([ new \App\Phone(['phone'=>344434]), new \App\Phone -01-30 08:11:18', '2020-01-30 08:11:18') insert into `stu__classes` (`cid`, `sid`) values (8, 1) 使用saveMany ()插入多个数据 public function show(){ $stu = Stu::find(1); $res = $stu->mclass()->saveMany 第二个参数插入值对应的额外字段(数组) public function show(){ $stu = Stu::find(1); $res = $stu->mclass()->saveMany
Comment 模型中 $post->comments()->save($comment); // 保存多个关联模型 $post = App\Post::find(1); $post->comments()->saveMany
find(1); $post->comments()->save($comment); 保存多个关联模型 $post = App\Post::find(1); $post->comments()->saveMany