Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vikas_developer's avatar

Eloquent Relationship help

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.

0 likes
17 replies
Nakov's avatar

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.

vikas_developer's avatar

@nakov Yes I have tried this one but its fetching all the users but I need only those who are having the orders along with them.

array:3 [ 0 => array:7 [ "id" => 1 "email" => "[email protected]" "is_approved" => 1 "created_at" => "2019-10-07 06:38:49" "updated_at" => "2019-10-07 06:38:49" "extra_orders" => array:1 [] ] 1 => array:7 [ "id" => 2 "email" => "[email protected]" "is_approved" => 1 "created_at" => "2019-10-07 06:39:39" "updated_at" => "2019-10-07 06:39:39" "extra_orders" => array:2 [] ] 2 => array:7 [ "id" => 3 "email" => "[email protected]" "is_approved" => 1 "created_at" => "2019-10-07 06:40:00" "updated_at" => "2019-10-07 06:40:00" "extra_orders" => [] ] ]

Nakov's avatar

@vikas_developer then you just need to append a simple condition like this:

User::with('extraOrders')->has('extraOrders')->get();

has() will only get you the users that have at least one extra order.

vikas_developer's avatar

@nakov can you please guide how can I access the order items object inside the extra_orders object.

User::with('extraOrders')->has('extraOrders')->get();

    @foreach($extra_orders as $order)
    <tr>
        <td>{{ $order->orders->item }}</td>
    </tr>
@endforeach
Nakov's avatar

@vikas_developer, so you are passing $users collection to the view, you then need to iterate over that collection and access it's extraOrders in order to proceed accessing the orders from each extra order, a bit complex, but something like this:

$users = User::with('extraOrders')->has('extraOrders')->get();

return view('pages.order_received', compact('users'));

Then in the view:

@foreach ($users as $user)
    @foreach ($user->extraOrders as $extraOrder)
        <tr>
            <td>{{ $extraOrder->order->item }}</td>
        </tr>
    @endforeach 
@endforeach 
vikas_developer's avatar

@nakov , Now I am fetching all the order items in one place with extra orders so that I can place the quantity and checkbox checked so that particular user. below code will fetch the whole orders item with list of orders placed.

public function orderPlace() { $orders = Orders::with(['ExtraOrders'])->get(); return view('pages.order_place',compact('orders')); }

@foreach($orders as $order) @foreach($order->ExtraOrders as $userOrder)

@endforeach

@endforeach

Getting error : Invalid argument supplied for foreach() (View: D:\xampp\htdocs\portal\resources\views\pages\order_place.blade.php)

Nakov's avatar

@vikas_developer the error again gives you a clue on what is going on. In your Orders model do you have a relationship setup called ExtraOrders? Can you show the code from the Orders model completely?

Surround a code with three ``` from each side, so it looks better here on the forum.

vikas_developer's avatar

@nakov Thanks for the suggestion. Please find below Orders Model code

 public function ExtraOrders()
    {
        return $this->hasOne(ExtraOrders::class,'order_id');
    }
Nakov's avatar
Nakov
Best Answer
Level 73

@vikas_developer so hasOne returns only one extra order, so you cannot use foreach on one item. Should it be hasMany instead?

vikas_developer's avatar

@nakov Thanks it's working but I want to confirm that should I run the same foreach in a table multiple times if I want to get the ExtraOrders items

Nakov's avatar

@vikas_developer if you want to get a list of extra orders then hasMany is required, then you can use foreach to display all of them connected to that order. Otherwise if you use just one, there is not need of foreach.

Hope this explains more.

vikas_developer's avatar

@nakov I want to print the selected orders by logged in user with orders and extraorderslist

public function orderItems()
    {   
        $myItems = ExtraOrders::where('user_id',Auth::user()->id)->with('orders')->get();
        dd($myItems->toArray());
        return view('pages.order_items',compact('myItems'));
    }
Nakov's avatar

@vikas_developer you can start a new discussion on the forum, and share exactly what you need. Because we are going very far from the initial question here. So if that one is answered then start a new conversation, someone else might help as well.

Please or to participate in this conversation.