2 回答
TA贡献1712条经验 获得超3个赞
您应该在 foreach 中创建 CityPost 模型:
$post = Post::all();
foreach($post as $p){
$store = new CityPost;
$store->user_id = 14;
$store->title = $v->title;
$store->desc = $v->desc;
$store->save();
}
dd($store);
TA贡献1900条经验 获得超5个赞
你可以做的是尝试不同的方式,如下所示
$post = Post::all();
$stores = [];
foreach($post as $p){
$store = new CityPost;
$store->user_id = 14;
$store->title = $v->title;
$store->desc = $v->desc;
$stores[] = $store->save();
//if $store->save() is boolean then you can use create method use save() or create() only one
$new = CityPost::create(['user_id'=>14,'title'=>$p->title,'desc'=>$p->desc]);
$stores[] = $new;
}
dd($stores);
或者您可以尝试另一种方式,例如批量更新,而无需多次调用DB
$post = Post::all();
$data = [];
foreach($post as $p){
$store->user_id = 14;
$store->title = $v->title;
$store->desc = $v->desc;
$data[] = ['user_id'=>$store->user_id,'title'=>$p->title,'desc'=>$p->desc];
}
CityPost::insert($data);
- 2 回答
- 0 关注
- 160 浏览
添加回答
举报