I was trying to mass update the record with the same foreign key. But I encounter a problem. If I use
$products = $products::findorfail($prod_id);
It manages to update a single column. Then when I trying using
$products = $products::query()->where('product_id', $prod_id)->get();
I encounter "Method Illuminate\\Database\\Eloquent\\Collection::options does not exist.", How do I overcome this problem.
Here is my current code
public function update(array $data = [], $prod_id, Variation $products)
{
foreach ($data['variants'] as $product) {
$products = $products::query()->where('product_id', $prod_id)->get();
$products->options()->detach(($product['option_id']));
$products->options()->attach(($product['option_id']));
$products->fill(Arr::set($data, 'product_id', $prod_id));
$products->fill(Arr::only($product, $products->getFillable()));
}
return $products->save();
}
Thanks in advance.