我使用的是Laravel 5.3,我试图将HasManyThrought
关系与where
声明一起使用,但我不知道如何实现这一目标。
所以我有这些表:
Users:
+----+-------+
| id | name |
+----+-------+
| 1 | James |
+----+-------+
Orders:
+----+---------+------------+
| id | user_id | state |
+----+---------+------------+
| 1 | 1 | complete |
+----+---------+------------+
| 2 | 1 | incomplete |
+----+---------+------------+
| 3 | 1 | pending |
+----+---------+------------+
Order Items:
+----+----------+--------+
| id | order_id | name |
+----+----------+--------+
| 1 | 1 | Mac |
+----+----------+--------+
| 2 | 1 | Tablet |
+----+----------+--------+
| 3 | 2 | Laptop |
+----+----------+--------+
现在我想获取用户的订单商品,所以我会做这样的事情:
public function items()
{
$this-hasManyThrough(Item::class, Order::class)
}
但是!如果我想提取仅他们的订单被列为complete
的商品?
答案 0 :(得分:2)
如果您希望获得@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rva);
//initialize//
mSidewalk = (CardView) findViewById(R.id.rva_CV);
//listen to cardview click ------------(done change here)
mSidewalk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RVA. this, RVA_Detail.class);
startActivity(intent);
}
});
州的所有经过身份验证的用户,请使用whereHas()
方法:
complete
不要忘记在$items = Item::whereHas('order', function($q) {
$q->where('state', 'complete')
->where('user_id', auth()->user()->id);
})->get();
模型中定义order()
关系:
Item