保存 laravel 中 table 行的数据
Save data from table rows in laravel
当我尝试保存时,我在一个页面中有多个同名的输入字段,如果我将来自多个 table 行数据的数据保存到数据库中,我会抛出一个 error.how。
错误
从空值创建默认对象
这里 $varientaccesss->variant_id = $request->input('varient'),
控制器
public function storevarient_update(Request $request)
{
//return $request;
$varientprice = new Price();
$varientprice->variant_id = $request->input('varient');
$varientprice->discount = $request->input('discount');
$varientprice->price = $request->input('vehicleprice');
$varientprice->save();
$varientaccess = new Categoryvariantmapping();
foreach ($varientaccess as $varientaccesss){
$varientaccesss->variant_id = $request->input('varient');
$varientaccesss->cat_id = $request->input('test');
$varientaccesss->rate = $request->input('cat_price');
$varientaccesss->status = '0';
$varientaccess->save();
}
return redirect('Varientupdate')->with('success', 'Details Added Successfully');
}
dd($request->all);
你可以这样保存
public function storevarient_update(Request $request)
{
//return $request;
$varientprice = new Price();
$varientprice->variant_id = $request->input('varient');
$varientprice->discount = $request->input('discount');
$varientprice->price = $request->input('vehicleprice');
$varientprice->save();
$cat_id = $request->input('cat_price');
$test_ids = $request->input('test');
foreach($test_ids as $key => $test)
{
$varientaccesss = new Categoryvariantmapping();
$varientaccesss = new Categoryvariantmapping();
$varientaccesss->variant_id = $request->input('varient'),
$varientaccesss->cat_id = $test,
$varientaccesss->rate = $cat_id[$key],
$varientaccesss->status = '0',
$varientaccess->save();
}
return redirect('Varientupdate')->with('success', 'Details Added Successfully');
}
这样做的目的是初始化一个你即将保存在数据库中的新数据
$varientaccess = new Categoryvariantmapping();
但是你把它放在了循环之外
$varientaccess = new Categoryvariantmapping();
foreach(....)
这意味着无论 foreach 中有多少行,它都只会保存一次,因为它不会再次初始化
所以解决办法是把它放在循环中
foreach(....) {
$varientaccess = new Categoryvariantmapping();
$varientaccesss->variant_id = $request->input('varient');
$varientaccesss->cat_id = $request->input('test');
$varientaccesss->rate = $request->input('cat_price');
$varientaccesss->status = '0';
$varientaccess->save();
}
当我尝试保存时,我在一个页面中有多个同名的输入字段,如果我将来自多个 table 行数据的数据保存到数据库中,我会抛出一个 error.how。 错误 从空值创建默认对象 这里 $varientaccesss->variant_id = $request->input('varient'),
控制器
public function storevarient_update(Request $request)
{
//return $request;
$varientprice = new Price();
$varientprice->variant_id = $request->input('varient');
$varientprice->discount = $request->input('discount');
$varientprice->price = $request->input('vehicleprice');
$varientprice->save();
$varientaccess = new Categoryvariantmapping();
foreach ($varientaccess as $varientaccesss){
$varientaccesss->variant_id = $request->input('varient');
$varientaccesss->cat_id = $request->input('test');
$varientaccesss->rate = $request->input('cat_price');
$varientaccesss->status = '0';
$varientaccess->save();
}
return redirect('Varientupdate')->with('success', 'Details Added Successfully');
}
dd($request->all);
你可以这样保存
public function storevarient_update(Request $request)
{
//return $request;
$varientprice = new Price();
$varientprice->variant_id = $request->input('varient');
$varientprice->discount = $request->input('discount');
$varientprice->price = $request->input('vehicleprice');
$varientprice->save();
$cat_id = $request->input('cat_price');
$test_ids = $request->input('test');
foreach($test_ids as $key => $test)
{
$varientaccesss = new Categoryvariantmapping();
$varientaccesss = new Categoryvariantmapping();
$varientaccesss->variant_id = $request->input('varient'),
$varientaccesss->cat_id = $test,
$varientaccesss->rate = $cat_id[$key],
$varientaccesss->status = '0',
$varientaccess->save();
}
return redirect('Varientupdate')->with('success', 'Details Added Successfully');
}
这样做的目的是初始化一个你即将保存在数据库中的新数据
$varientaccess = new Categoryvariantmapping();
但是你把它放在了循环之外
$varientaccess = new Categoryvariantmapping();
foreach(....)
这意味着无论 foreach 中有多少行,它都只会保存一次,因为它不会再次初始化
所以解决办法是把它放在循环中
foreach(....) {
$varientaccess = new Categoryvariantmapping();
$varientaccesss->variant_id = $request->input('varient');
$varientaccesss->cat_id = $request->input('test');
$varientaccesss->rate = $request->input('cat_price');
$varientaccesss->status = '0';
$varientaccess->save();
}