我在Laravel中有3个模型:data is <br/>
data is {"product":{"description":"","productPrice":"","isTaxable":""},"quantity":"","discount":"","finalPrice":"","lineItemTotal":""}
8:352 data is {"delete":"<i class='fa fa-pencil-square' aria-hidden='true'></i> <i class='fa fa-minus-square .js-delete' aria-hidden='true'></i>","id":21,"quantity":12,"discount":12,"product":{"id":501,"upc":null,"description":"Eggs","restockQty":26,"productPrice":40000,"isTaxable":false,"productCost":11000,"image":"","productCode":"01 B 04","packing":20,"productWholesalePrice":20000},"productId":501,"salesOrder":null,"salesOrderId":8,"lineTaxes":9,"isTaxable":true,"lineItemTotal":999,"finalPrice":9999}
,Contracter
和Contractor_Areas
Contractor_Trade_Type
可以具有贸易类型(水暖,电力等)以及按邮政编码工作的区域。
所以我需要选择所有Contractor
和某个contractors
和trade
。
因此,代码需要对postcode
Contractor_Trade_Type
(在这种情况下为id
)和此处的邮政区域16
进行以下操作,并给我匹配的承包商输入ID和邮政编码。
但是它说:
找不到trade_type_id列
SS
模型如下:
$con = App\Contractor::with('Contractor_Trade_Types')->where('trade_type_id' , 16 )->with( 'contractor_areas' )->where('contractor_areas.postcode','ss')->first();
print_r( $con );
答案 0 :(得分:1)
据我所知,您正在尝试链接with()
和where()
子句以便在关系中进行搜索。
您可以在with()查询方法中使用closure
添加子查询:
$con = App\Contractor::with(['contractor_trade_types' => function ($query) {
$query->where('id', 16);
}])
->with(['contractor_areas' => function($query) {
$query->where('id', $area_id);
}])
->first();
print_r($con);
此外,这些检查可以添加到query scope中以在您的应用程序中使用:
$con = App\Contractor::withTradeType(16)->withArea('ss')->first();
public function scopeWithTradeType($query, $trade_type_id)
{
return $query->with(['contractor_trade_types' => function ($q) {
$q->where('id', $trade_type_id);
}]);
}
public function scopeWithArea($query, $area_id)
{
return $query->with(['contractor_areas' => function($q) {
$q->where('id', 'ss');
}]);
}