7 months ago

Eloquent Relationship help

Posted 7 months ago by vikas_developer

I have three tables :

  1. User => [ id, email, password, timestamps ]
  2. extra_orders => [ id, user_id, order_id, quantity, status, timestamps ]
  3. orders => [id, item, image, price, timestamps ]

User Model.

public function extraOrders() { return $this->hasMany(ExtraOrders::class); }

ExtraOrder Model.

public function user() { return $this->belongsTo(User::class, 'user_id', 'id'); }

public function order()
    return $this->belongsTo(Orders::class);

Orders Model.

public function user() { return $this->belongsTo(User::class); }

In controller I am trying to get data using below method

public function orderReceived() {

    $extra_orders = ExtraOrders::with(['user','orders'])->get();
    return view('pages.order_received',compact('extra_orders'));

all the data is coming but I want to merge the the user extraorders if user id is same

Please check and help.

Thanks in advance.

Please sign in or create an account to participate in this conversation.