nanadjei2's avatar

Multiple update form input with hasMany relationship

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

0 likes
6 replies
mushood's avatar

Hello

First of all, you are getting the error in your controller file or your html. If you are not sure, please paste the whole error.

Assuming it is in the controller, you are trying to access $option->id, where $option represents only the value of the id and NOT the object itself.

Basically should look like this:

$orderProduct  = $order->orderProduct()->where('option_id', $option)->update(['option_id' => $request->option]);

Hope this helps

1 like
nanadjei2's avatar

Thank you but am now getting this error:

array to string conversion (SQL: updateorder_productssetoption_id= 1,updated_at= 2018-01-03 14:11:04 whereorder_products.order_id= 1 andorder_products.order_idis not null andoption_id= 1)

mushood's avatar

Thats because orderProduct() is returning an array

$orderProducts  = $order->orderProduct()->where('option_id', $option)->get();

foreach($orderProducts as $orderProduct){
    $orderProduct->update(['option_id' => $request->option]);
    $orderProduct->save();
}

I did not test this code. It should be something along this line.

nanadjei2's avatar

The success message is returning but the record is not updating

nanadjei2's avatar

Ok I have managed to work around it and my new error is:

Array to string conversion (SQL: updateorder_productssetoption_id= 1,updated_at= 2018-01-03 17:10:16 whereid= 1)

Please or to participate in this conversation.