我有3张桌子:
users: (App\Users)
id|name|email
coupons: (App\Coupons)
id|coupon_code|user_id|discount
orders: (App\Orders)
id|order_id|total|coupon_code
我需要将用户与订单联系起来,所以我可以按订单数排序,我尝试这样:
命令功能belogns到App \ User
public function orders()
{
return $this->hasManyThrough('App\Coupons', 'App\Orders', 'coupon', 'coupon_code');
}
有没有办法获得这些表的关系,所以我可以使用:
$users = App\Users->withCount('orders')->orderBy('orders_count', 'desc');
我可以通过以下方式得到正确的结果:
$coupons = App\Coupons::where('user_id', '=', $this->id);
return App\Orders::whereIn('coupon', $coupons->pluck('coupon_code'));
但是我无法对此专栏进行排序
答案 0 :(得分:-1)
关于如何获得描述中的表格,获取关系的方式应如下:
ftable(xtabs(data = df), row.vars = 1:2, col.vars = 3:4)
# colsA G H I
# colsB J K L J K L J K L
#rowsA rowsB
#A D 1 0 0 0 3 1 1 1 0
# E 2 0 0 1 1 0 0 0 1
# F 5 0 0 3 2 1 0 1 1
#B D 0 1 1 1 0 3 1 1 1
# E 2 2 1 3 1 1 0 3 1
# F 1 1 2 3 3 0 1 2 1
#C D 0 2 3 1 2 0 4 3 2
# E 2 2 2 1 2 0 0 1 1
# F 1 0 1 2 0 1 2 1 2
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through