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?
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();
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']);
Well I got them like this:
$orders = self::$user->orders()->orderBy('order_id', 'asc')->get();
return View::make('site.users.orders', [
'orders' => $orders
]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community