[英]Laravel query replace new data with old one
Basically this question is following of [link]基本上这个问题是[链接]
As that question got too messy I want to separate the question here.由于这个问题太乱了,我想在这里分开这个问题。
I have appended rows in my blade where several select box will append and i can choose options of each of them.我在刀片中附加了行,其中将附加几个选择框,我可以选择每个选项。 this data will save in database ( Until here everything is good )此数据将保存在数据库中(直到这里一切都很好)
comes when I change one of my selected options and instead of being update it will add all selects as new one.当我更改我选择的选项之一而不是更新时,它会将所有选择添加为新选项。
controller
public function spacssendto(Request $request) {
$this->validate($request, array(
'product_id' => 'required',
'subspecifications' => 'required',
));
// get all selected option
$looped = $request->subspecifications;
$spec = [];
if(!empty($looped)){ //check if there is any select box without option
foreach($looped as $sub) {
$sub = Subspecification::find($sub);
if (!empty($sub->id)) {
$data = (
[
'product_id' => $request->product_id,
'subspecification_id' => $sub->id,
]
);
array_push($spec, $data);
}
}
}
dd($spec);
// DB::table('product_subspecification')->insert($spec); // save data to database
}
results of dd($spec)
dd($spec)
Any idea?任何的想法?
I've changed my function to code below and now everything working fine我已将我的功能更改为下面的代码,现在一切正常
public function spacssendto(Request $request) {
$this->validate($request, array(
'product_id' => 'required',
'subspecifications' => 'required',
));
$collection = collect($request->subspecifications)->map(function ($name) {
return strtoupper($name);
})
->reject(function ($name) {
return empty($name);
});
$product = Product::find($request->product_id);
$product->subspecifications()->sync($collection);
}
Hope it help others.希望它能帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.