Support the ongoing development of Laravel.io →
Session Database Blade

I'm trying to make a menu for user when he loggin to be able to check his order history. So far I got the page but it is empty. This is what I have in my controller.

public function viewOrders() {
    $user_order = self::$user->user_id;
    $orders = Order::where('user_id', '=', $user_order);

    return View::make('site.users.orders', [
        'orders' => $orders
    ]);
}

This is in User model

public function orders() {
    return $this->hasMany('Order', 'user_id', 'user_id');
}

And in Order model

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

What is the problem? Why I can't get orders for the user?

Last updated 3 years ago.
0

first: remove the user_id in the relations. It's the default key Eloquent will look for for that relation.

second:

$user_order = $this->user->id;

if you are using the built in authentication use the following to get the user

$user = Auth::user();
0

Thank's for the answer. Is still no data on the page but here is what I forgot to mention in my question. This is the button for orders view

<li><a href="{{ URL::to('/users/orders') }}?_token={{ csrf_token() }}">My Orders</a></li>

and this is the route Route::get('/users/orders', ['uses' => 'UsersController@viewOrders', 'before' => 'auth|csrf']);

0
Solution

Well I got them like this:

    $orders = self::$user->orders()->orderBy('order_id', 'asc')->get();
    return View::make('site.users.orders', [
        'orders' => $orders
    ]);
0

Sign in to participate in this thread!

Eventy

Your banner here too?

vinsbg vinsbg Joined 26 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.