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

vikas_developer's avatar

How to Print all selected items by logged in User

I have three tables :

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

User Model.

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

ExtraOrders Model.

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

    public function orders()
    {
        return $this->belongsTo(Orders::class,'id');
    }

Orders Model.

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

my controller code for printing ordered Items.


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

It's returning null. Please help.

0 likes
20 replies
Nakov's avatar

@vikas_developer so why not this way:

$myItems = auth()->user()->with('ExtraOrders.orders')->get();

Then in the view you will need to iterate over them?

So $myItems will be actually an instance of the User, which will contain collection of ExtraOrders and each extra order will contain it's orders.

1 like
vikas_developer's avatar

@nakov Its returning with all users data but I need only logged in user data also the orders are showing null value

Nakov's avatar

@vikas_developer that does not make sense.. This auth()->user() is the currently authenticated user.. So make sure that your relationship is setup correctly then.

Test the user using dd(auth()->user()) is it one or multiple? :)

I believe that your setup is a bit wrong. I believe that a user should have many orders, and each order can have an extra order, does this sounds correct?

vikas_developer's avatar

@nakov You are right. but don't know why its coming with wrong data.

for reference.


array:3 [▼
  0 => array:7 [▼
    "id" => 1
    "email" => "[email protected]"
    "role" => "admin"
    "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]"
    "role" => "manager"
    "is_approved" => 1
    "created_at" => "2019-10-07 06:39:39"
    "updated_at" => "2019-10-07 06:39:39"
    "extra_orders" => array:1 [▼
      0 => array:9 [▼
        "id" => 77
        "order_id" => 2
        "user_id" => 2
        "quantity" => 0
        "status" => 0
        "billing" => 0
        "created_at" => "2019-10-18 11:44:06"
        "updated_at" => "2019-10-18 11:44:06"
        "orders" => null
      ]
    ]
  ]
  2 => array:7 [▼
    "id" => 3
    "email" => "[email protected]"
    "role" => "exhibitor"
    "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 why do you use ->toArray() on it? You can simply iterate over the collection.

If the relationships should be as I described them above, change the methods then :)

Nakov's avatar

@vikas_developer it seems wrong, so I guess the relationships are wrong that's why you get data that you don't need.. Does auth()->user() returns the correct one?

vikas_developer's avatar

@nakov I have also checked the logged In user_id & it's showing correct id, so I think the relationships are wrong though can you please have a look on my code via remote access ?

Nakov's avatar

@vikas_developer the forum is for open help :) if I do consulting or private hours then I will need to be hired :D

Let's discuss it here, I am trying to understand your project here.

Should it be this way instead: A user has orders, an order can have many extra orders?

vikas_developer's avatar

@nakov Yes, A user can have many orders. I should be very simple but don't know why its creating problem.

Orders Table contains : all the items with price & Description. ExtraOrders Table : it contains the order_id user_id & quantity etc. User Table : it is having the list of users.

Are you clear with the Relationships created by me ?

auth()->user()->with('ExtraOrders.orders')->get();

Nakov's avatar

@vikas_developer Yes, it is clear, but you got it the wrong way.. I believe this should be your setup if you agree with what I've said above:

User.php

public function orders()
{
    return $this->hasMany(Order::class);
}

So this means that the orders table should have user_id.

Order.php

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

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

This means that the extra_orders table should have an order_id with a foreign key to the id on the orders table.

ExtraOrder.php

public function order()
{
    return $this->belongsTo(Order::class);
}

Then calling this:

auth()->user()->load('orders.extraOrders');

Should give you the expected result. Let me know :)

1 like
Nakov's avatar

@vikas_developer Sorry I made a slight change above. Instead of with() you should use load() because with with() again it will result in all users.

vikas_developer's avatar

@nakov Above statements are correct but only one scenario is different which is user_id in orders table as it contains only items price and description which is inserted manually. so the user will see a list of items coming from orders table having id,item name,price,quantity & etc. Then Extra Orders table is the main table where all the id,order_id which is foreign key of order table and price, quantity extra. So the user is connected to user_id in Extra Orders table and the order_id is connected to orders table.

Please confirm if you get the problem.

Nakov's avatar

@vikas_developer well to me it does not sound right, because I believe an order can exist even without extra order.. But I don't know your business. So if you keep the relationships as they were then try this instead.

auth()->user()->load('ExtraOrders.orders');
vikas_developer's avatar

@nakov Thanks for the understanding. I have used and it's working but the order items are coming null.

Nakov's avatar
Nakov
Best Answer
Level 73

@vikas_developer that means that your relationship between an extra order and order is not right, so you need to test that.. Do you have an order_id in your extra_orders table?

If you do have that name, just add this in the ExtraOrder model

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

then change the load to ExtraOrders.order play around with it, as that's what left...

vikas_developer's avatar

@nakov Yes, the order_id is in the extra_orders table.

After changing below code in ExtraOrder model I got the correct response.

public function orders()
    {
        return $this->belongsTo(Orders::class,'order_id');
    }

Thanks for the quick response.

Nakov's avatar

@vikas_developer please rename it to order as it returns One not Many. Even if you don't specify a second param it should still work, if you rename the method to just order.

And if the answer helped please mark it as Best Answer and enjoy coding :)

Please or to participate in this conversation.