Laravel 5.4:高级数据透视 table 查询问题
Laravel 5.4: Advanced pivot table query issue
我有两个模型:Order
和 Department
,通过多对多关系连接。此关系的主元 table 包含一个 "status" 字段。所以一个特定的订单可能看起来像:
- 制造:完成
- 包装:(不需要/不附)
- 发货:进行中
在我的应用程序的 UI 中,我有一个用于每个部门的选项卡和用于状态的复选框。因此 API 需要能够接受一个部门和多个可能状态的请求,以及 return 与给定部门的选定状态之一匹配的所有订单。
示例查询:/api/orders?dep=manufacturing&statuses=notStarted,inProgress
这需要 return 所有 "not started" 或 "in progress" 的订单制造部门(无论在任何其他部门的地位如何)
这是我写的查询:
$query = Order::with("departments");
$department = Request::get('department');
$statuses = explode(",", Request::get('statuses', ""));
if (!empty($department))
{
$query->whereHas('departments', function ($q) use ($department)
{
$q->where('name', $department);
});
if (count($statuses) > 0)
{
$query->where(function ($q) use ($department, $statuses)
{
foreach ($statuses as $status)
{
$q->orWhereHas('departments', function ($q) use ($department, $status)
{
$q->where('name', $department)->wherePivot('status', $status);
}
}
});
}
}
return $query->paginate(15);
这是抛出错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause'
我的关系定义如下:
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'order_statuses')->using('App\Models\OrderStatus')->withPivot('status')->withTimestamps();
}
默认情况下,只有模型键会出现在枢轴对象上。如果你的主元 table 包含额外的属性,你必须在定义关系时指定它们:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
我最终想出了以下解决方案:
if (!empty($departments)
{
if (count($statuses) > 0)
{
$query->whereHas('departments', function ($q) use ($department, $statuses)
{
$q->where('name', $department)->whereIn('order_statuses.status', $statuses);
}
} else {
$query->whereHas('departments', function ($q) use ($department)
{
$q->where('name', $department);
}
}
}
我有两个模型:Order
和 Department
,通过多对多关系连接。此关系的主元 table 包含一个 "status" 字段。所以一个特定的订单可能看起来像:
- 制造:完成
- 包装:(不需要/不附)
- 发货:进行中
在我的应用程序的 UI 中,我有一个用于每个部门的选项卡和用于状态的复选框。因此 API 需要能够接受一个部门和多个可能状态的请求,以及 return 与给定部门的选定状态之一匹配的所有订单。
示例查询:/api/orders?dep=manufacturing&statuses=notStarted,inProgress
这需要 return 所有 "not started" 或 "in progress" 的订单制造部门(无论在任何其他部门的地位如何)
这是我写的查询:
$query = Order::with("departments");
$department = Request::get('department');
$statuses = explode(",", Request::get('statuses', ""));
if (!empty($department))
{
$query->whereHas('departments', function ($q) use ($department)
{
$q->where('name', $department);
});
if (count($statuses) > 0)
{
$query->where(function ($q) use ($department, $statuses)
{
foreach ($statuses as $status)
{
$q->orWhereHas('departments', function ($q) use ($department, $status)
{
$q->where('name', $department)->wherePivot('status', $status);
}
}
});
}
}
return $query->paginate(15);
这是抛出错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause'
我的关系定义如下:
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'order_statuses')->using('App\Models\OrderStatus')->withPivot('status')->withTimestamps();
}
默认情况下,只有模型键会出现在枢轴对象上。如果你的主元 table 包含额外的属性,你必须在定义关系时指定它们:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
我最终想出了以下解决方案:
if (!empty($departments)
{
if (count($statuses) > 0)
{
$query->whereHas('departments', function ($q) use ($department, $statuses)
{
$q->where('name', $department)->whereIn('order_statuses.status', $statuses);
}
} else {
$query->whereHas('departments', function ($q) use ($department)
{
$q->where('name', $department);
}
}
}