我正在尝试将数组插入到DB(具有相同FK(product_id)的双行),我有父表(Products)和子表(Product_translation)。 我有2个模型与关系,并获得产品表的lastinserted Id,但现在我的问题是它保存单词(数组)到DB,而不是在这个数组上循环使用laravel形式插入correclty:这里是我的表单
{!!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" )!!}
和控制器功能在这里:
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);
}
但是我收到了这个错误:
从空值创建默认对象
这是指那一行:
现在更新我将数组存储到我的数据库时遇到问题:
我的表格给我这个数组:
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"
]
]
我需要将其保存到数据库,但事实并非如此!
我在控制器上使用此代码:
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);
}
当我从创建行
中删除评论时ProductsTranslation ::创建($ insertprod);
它给我一个无法更新或插入FK Product_id的错误!
如何将该数组保存到我的数据库?
答案 0 :(得分:0)
请使用DB类将您的代码更改为此代码(此表中的“插入所有数组”)..
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);
}
如果您想要使用产品转换的模式而不是替换它..
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();
}
}