dpjj4763 2017-03-24 10:54
浏览 47

以laravel形式将数组插入DB

I'm trying to insert array to DB ( double rows with the same FK (product_id) ) , which i have parent table(Products) and child table(Product_translation) . I have 2 Model with relationship and getting the lastinserted Id of the product table , but the issue with me now is it's save word(array) to DB , instead of loop on this array to insert it correclty using laravel form : and here's my form

 {!!Form::text('title[]'," ",["class"=>"form-control col-md-7 col-xs-12" )!!} 

 {!!Form::text('language[]'," ",["class"=>"form-control col-md-7 col-xs-12" ])!!} 

  {!!Form::text('title[]'," ",["class"=>"form-control col-md-7 col-xs-12" )!!} 

  {!!Form::text('language[]'," ",["class"=>"form-control col-md-7 col-xs-12" )!!} 

and the controller function here's :

  function store( ){
            $parentproduct = new Product();
            $parentproduct->id = Input::get('id');
            $parentproduct->save();
            $insertedId = $parentproduct->id;
            $langArray = Input::get('language');
            $titleArray = Input::get('title');
             $count = count($titleArray);
            for($i = 0; $i < $count; $i++)

        { 

        $product = new ProductsTranslation(array('language' => $langArray[$i], 'title' => $titleArray[$i],
        $product->product_id = $insertedId));
        $product->save();
        }
            DB::table('products_translations')->insert($product);
        }

But I'm getting this error:

Creating default object from empty value

Which is referring to that line :

$product->product_id = $insertedId

Updated now i have a problem to store the array to my database :

My form getting me this array :

array:2 [▼
  0 => array:4 [▼
    "id" => null
    "product_id" => 225
    "title" => "English Title"
    "language" => "En"
  ]
  1 => array:4 [▼
    "id" => null
    "product_id" => 225
    "title" => "Arabic title"
    "language" => "Ar"
  ]
]

And i need to save this to database but it didn't !

which i'm using this code at the controller :

function store(Request $request) {
    $parentproduct = new Product();
    $parentproduct->id = Input::get('id');
    $parentproduct->save();
    $insertedId = $parentproduct->id;

     foreach ($request->input('language') as $i=>$language) {

         $insertprod[] = array(
        'id' =>$request->input('id')[$i], 
       'product_id'=>$parentproduct->id,
        'title' =>$request->input('title')[$i],
        'language' => $request->input('language')[$i],    
         );
    DB::table('products_translations')->insert($insertprod); 

     }
     ProductsTranslation::insert($insertprod); 
     DB::table('products_translations')->insert($insertprod);
    // DB::table('products_translations')->update(['change' => Input::all()]);
      DB::table('products_translations')->insert($insertprod);
     //ProductsTranslation::create($insertprod);
      dd($insertprod); 

}

When i remove the comment from the line of create

ProductsTranslation::create($insertprod);

It's getting me an error that can't update or insert FK Product_id !

How can save that array to my db ?

  • 写回答

1条回答 默认 最新

  • dongyu1979 2017-03-24 11:26
    关注

    Please change your code to this one ( This Insert All Array in your table) with ther use of DB class ..

    function store( ){
            $parentproduct = new Product();
            $parentproduct->id = Input::get('id');
            $parentproduct->save();
            $product = array();
            $insertedId = $parentproduct->id;
            $langArray = Input::get('language');
            $titleArray = Input::get('title');
             $count = count($titleArray);
            for($i = 0; $i < $count; $i++){ 
               $product[]  = array(
                        'language'=>$langArray[$i],
                        'title' =>$titleArray[$i],
                        'product_id'=> $insertedId );
    
             }
            DB::table('products_translations')->insert($products);
        }
    

    If you want to user modal of producttranslations than replace this ..

    function store( ){
            $parentproduct = new Product();
            $parentproduct->id = Input::get('id');
            $parentproduct->save();
            $insertedId = $parentproduct->id;
            $langArray = Input::get('language');
            $titleArray = Input::get('title');
             $count = count($titleArray);
            for($i = 0; $i < $count; $i++){ 
    
               $product = ProductsTranslation::firstOrCreate(array('language' =>   $langArray[$i], 'title' => $titleArray[$i], 'product_id'=>$insertedId));
               or 
               $product = new ProductsTranslation();
    
    
               $product->language =$langArray[$i];
               $product->title =$titleArray[$i];
               $product->product_id =$insertedId;
               $product->save();
    
             }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂