在laravel中显示用户的订单 [英] Showing orders of user in laravel
本文介绍了在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.
这是我的控制器
public function viewOrders() {
$user_order = self::$user->user_id;
$orders = Order::where('user_id', '=', $user_order);
return View::make('site.users.orders', [
'orders' => $orders
]);
}
我在这里正确获得了user_id吗?我不确定...
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');
}
推荐答案
好,因此,根据您的route + button + model这样做
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
]);
这应该工作.如果不需要,可以删除orderBy子句.
this should work.. You can remove orderBy clause if you don't need it.
这篇关于在laravel中显示用户的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文