And why not getting them through the user instead? So it will give you list of orders for each user instead?
For example:
User::with('extraOrders')->get();
Then in the view you will iterate over each user and show all the orders per user.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have three tables :
public function extraOrders() { return $this->hasMany(ExtraOrders::class); }
public function user() { return $this->belongsTo(User::class, 'user_id', 'id'); }
public function order()
{
return $this->belongsTo(Orders::class);
}
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.
@vikas_developer so hasOne returns only one extra order, so you cannot use foreach on one item. Should it be hasMany instead?
Please or to participate in this conversation.