Stan Stan - 5 months ago 18
PHP Question

Showing orders of user in laravel

I'm trying to give ability on user to see his orders. How can I query the database.. I'm trying something like this but I got empty page.. I mean nothing from database. May be my query ins't correct.

This is 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
]);
}


Am I getting correctly user_id here? I'm not sure...

Update: I have this in my User model

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

Answer

Ok, so based on your route+button+model do it like this

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

this should work.. You can remove orderBy clause if you don't need it.

Comments