我正在尝试将数组插入 DB(具有相同 FK (product_id) 的双行),我有父表(Products)和子表(Product_translation)。 我有 2 个具有关系的模型并获取产品表的 lastinserted Id,但我现在的问题是将 word(array) 保存到 DB,而不是在此数组上循环以使用 laravel 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" )!!}
这里的 Controller 函数是:
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);
}
但是我收到了这个错误:
Creating default object from empty value
指的是那一行:
$product->product_id = $insertedId
现在更新了,我在将数组存储到我的数据库时遇到了问题:
我的表格让我得到这个数组:
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"
]
]
我需要将它保存到数据库,但它没有!
我在 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);
}
当我从创建行中删除注释时
ProductsTranslation::create($insertprod);
给我一个无法更新或插入 FK Product_id 的错误!
如何将该数组保存到我的数据库中?
最佳答案
请使用 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);
}
如果你想使用 producttranslations 的用户模式而不是替换这个..
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();
}
}
关于php - 以laravel形式将数组插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997653/