我有三个加入的模型。我需要在workspace_members
为1(即有效)时获取inventory.status
表详细信息。
我怎样才能在古怪的语言中写下这个条件。以下是我的查询。提前谢谢。
$data['inventories'] = Inventory::
Join('category_info','inventory.category_id','=','category_info.category_id')
->leftJoin('workspace_member_inventory', 'inventory.inventory_id', '=', 'workspace_member_inventory.inventory_id')
->leftJoin('workspace_members','workspace_member_inventory.member_id','=','workspace_members.member_id')
->where('inventory.workspace_id', '=', $workspace_id)
->select('inventory.*', 'workspace_member_inventory.member_id','workspace_members.*','category_info.category_name', 'inventory.status as status')
->get()
->toArray();
答案 0 :(得分:0)
我不知道我是否理解,但我认为您正在寻找Laravel Query Scopes
在您的库存模型上写下范围:
public function scopeIsActive($query,$status = null){
if(!is_null($status)){
if($status == 1){
return $query->leftJoin('workspace_members','workspace_member_inventory.member_id','=','workspace_members.member_id')->where('inventory.status',1);
}else{
return $query->where('inventory.status',0);
}
}else{
return $query;
}
}
然后将其添加到您的查询中:
$data['inventories'] = Inventory::
Join('category_info','inventory.category_id','=','category_info.category_id')
->leftJoin('workspace_member_inventory', 'inventory.inventory_id', '=', 'workspace_member_inventory.inventory_id')
->where('inventory.workspace_id', '=', $workspace_id)
->isActive(1)
->select('inventory.*', 'workspace_member_inventory.member_id','workspace_members.*','category_info.category_name', 'inventory.status as status')
->get()->toArray();