Laravel 集团优先事项(和/或)
我在确定工作重点和/或优先级时遇到困难 让我用代码解释一下 我有三张桌子: 产品Laravel 集团优先事项(和/或),laravel,Laravel,我在确定工作重点和/或优先级时遇到困难 让我用代码解释一下 我有三张桌子: 产品 id无符号整数自动递增 乘积VARCHAR(255) 客户 id无符号整数自动递增 客户端VARCHAR(255) 客户产品 客户端id无符号整数 乘积\u id无符号整数 价格小数(9,2)空 有效的\u from(DATETIME)NULL 在(日期时间)之前有效\u为空 我想为客户找出特定日期的产品价格 我在产品模型中有一个范围: public function clients() { r
- id无符号整数自动递增
- 乘积VARCHAR(255)
- id无符号整数自动递增
- 客户端VARCHAR(255)
- 客户端id无符号整数
- 乘积\u id无符号整数
- 价格小数(9,2)空
- 有效的\u from(DATETIME)NULL
- 在(日期时间)之前有效\u为空
public function clients()
{
return $this->belongsToMany('Client', 'client_product', 'product_id', 'client_id')
->withPivot('price', 'valid_from', 'valid_until');
}
如果产品有价格,则:
- valid_from已设置日期,不能为空
- 有效的\u until可以为空或设置了日期
public function findPrice($clientId, $date)
{
return $this->clients()
->where('client_id', $clientId)
->where('valid_from', '<=', $date)
->where(function($query) use ($date) {
$query->wherePivot('valid_until', '>=', $date)
->orWherePivot('valid_until', '=', NULL);
})
->first();
}
还有,我不能用
public function findPrice($clientId, $date)
{
return $this->clients()
->where('client_id', $clientId)
->where('valid_from', '<=', $date)
->wherePivot(function($query) use ($date) {
$query->where('valid_until', '>=', $date)
->orWhereNull('valid_until');
})
->first();
}
因为“wherePivot”方法只将字符串作为第一个参数,而不是将闭包作为“where”方法
BelongsToMany wherePivot(string $column, string $operator = null, mixed $value = null, string $boolean = 'and')
您可以不使用WestEvIt来做,只使用查询中的“()),并指定具有列名的中间表名,因此在WHERE子句中,它会考虑列在中间表中,以供Ex.P/P>使用。
public function findPrice($clientId, $date)
{
return $this->clients()
->where('client_id', $clientId)
->where('valid_from', '<=', $date)
->where(function($query) use ($date) {
$query->where('client_product.valid_until', '>=', $date)
->orWhere('client_product.valid_until', '=', NULL);
})
->first();
}
公共函数findPrice($clientId,$date)
{
返回$this->clients()
->其中('client_id',$clientId)
->其中('valid_from','=',$date)
->orWhere('client_product.valid_until','=',NULL);
})
->第一个();
}
考虑客户产品的有效期
这应该工作,筛选器将应用于中间表。
您可以在不使用WestEvIt的情况下使用它,只使用查询中的“()),并指定具有列名的中间表名,以便在WHERE子句中考虑列为Ext.< /P>中表。public function findPrice($clientId, $date)
{
return $this->clients()
->where('client_id', $clientId)
->where('valid_from', '<=', $date)
->where(function($query) use ($date) {
$query->where('client_product.valid_until', '>=', $date)
->orWhere('client_product.valid_until', '=', NULL);
})
->first();
}
公共函数findPrice($clientId,$date)
{
返回$this->clients()
->其中('client_id',$clientId)
->其中('valid_from','=',$date)
->orWhere('client_product.valid_until','=',NULL);
})
->第一个();
}
考虑客户产品的有效期
这应该有效,并且过滤器将应用于中间表。如果答案有效,请将其标记为正确,以便其他用户可以获得有关该问题的参考。…)如果答案有效,请将其标记为正确,以便其他用户可以获得有关该答案的参考信息:)
public function findPrice($clientId, $date)
{
return $this->clients()
->where('client_id', $clientId)
->where('valid_from', '<=', $date)
->where(function($query) use ($date) {
$query->where('client_product.valid_until', '>=', $date)
->orWhere('client_product.valid_until', '=', NULL);
})
->first();
}