laravel中如何使用foreach将数据添加到数据库中

mis*_*sry 1 php mysql sql laravel laravel-5

我想将表中的所有数据移入post表中CityPost,我使用foreach但为什么只有一个数据条目,

 $store = new CityPost;
           $post = Post::all();
           foreach($post as $p){
               $store->user_id = 14; 
               $store->title = $v->title;
               $store->desc = $v->desc;
               $store->save();
           }
           dd($store);
Run Code Online (Sandbox Code Playgroud)

我希望表user_id中的所有内容PostCity都是 14

Au *_*yen 5

您应该在 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);
Run Code Online (Sandbox Code Playgroud)