Laravel - 按 "a Field and Its Relationship Model Field" 之间的条件过滤模型
Laravel - Filtering a Model by Condition between "a Field and Its Relationship Model Field"
我有一对一关系的这两个模型。
“产品”
- id
- 名字
- minimum_required
"product_data"
- id
- product_id
- 价格
- oh_hand
I want to get the count of product_data where its on_hand is less than
its related product's minimum_required.
我已经尝试了子查询,但我仍然无法弄清楚。我想要的查询可能看起来像这样。
$low_products_count = ProductDetail::where('on_hand', '<', Product::select('minimum_required')->count();
您可以加入表格然后使用 'whereColumn':
$low_products_count =Product::join('product_data','product_data.product_id','=',
'products.id')->whereColumn('product_data.on_hand','<','products.minimum_required')->get();
我对 eloquent 没有经验,所以在这里,我与查询构建器分享我的知识。
你可以这样做->
$low_products_count=DB::table('products')
->join('product_data','product_data.product_id','=',
'product.id')
->where('product_data.on_hand','<','products.minimum_required')->get();
你可以使用这个概念。
我有一对一关系的这两个模型。
“产品”
- id
- 名字
- minimum_required
"product_data"
- id
- product_id
- 价格
- oh_hand
I want to get the count of product_data where its on_hand is less than its related product's minimum_required.
我已经尝试了子查询,但我仍然无法弄清楚。我想要的查询可能看起来像这样。
$low_products_count = ProductDetail::where('on_hand', '<', Product::select('minimum_required')->count();
您可以加入表格然后使用 'whereColumn':
$low_products_count =Product::join('product_data','product_data.product_id','=',
'products.id')->whereColumn('product_data.on_hand','<','products.minimum_required')->get();
我对 eloquent 没有经验,所以在这里,我与查询构建器分享我的知识。
你可以这样做->
$low_products_count=DB::table('products')
->join('product_data','product_data.product_id','=',
'product.id')
->where('product_data.on_hand','<','products.minimum_required')->get();
你可以使用这个概念。