Am developing an E-commerce app and I want to update a table with an multiple data from form dynamically. The OrderProduct class is like a cart which will host the products of an order.
class OrderProduct extends Model
{
protected $fillable = ['product_id', 'product_category_id', 'option_id', 'quantity', 'sales_price', 'sub_total'];
}
This is my Order model:
class Order extends Model
{
protected $fillable = ['user_id', 'order_product_id', 'payment_method', 'amount_paid', 'billing_details', 'status', 'date'];
public function orderProduct() {
return $this->hasMany('App\OrderProduct');
}
public function products() {
return $this->hasMany('App\Product', 'product_id');
}
public function options() {
return $this->hasMany('App\Option', 'option_id');
}
}
This is the html;
<select class="form-control" id="option" name="option[]" id="tags">
@foreach($options as $option)
<option value="{{ $option->id }}" @if($product->option->name == $option->name) selected @endif>
{{ $option->name }}
</option>
@endforeach
</select>
This is my controller;
$order = Order::where('id', '=', $order->id)->firstOrFail();
foreach ($request->option as $option) {
$orderProduct = $order->orderProduct()->where('option_id', $option->id)->update(['option_id' => $request->option]);
}
I keep getting this error Trying to get property of non-object