【发布时间】:2020-10-14 10:35:41
【问题描述】:
我有一张表organisations,其中包含许多列,即id、name、created_at 和updated_at。
在我的Organisation 模型中,我通过在模型中添加以下代码来添加自定义属性:
// Add custom attributes
protected $appends = [
'device_count',
];
和:
public function getDeviceCountAttribute()
{
// Count organisation's devices
$device_count = Device::where('organisation_id', '=', $this->id)->count();
return $device_count;
}
在我的控制器中,我试图通过device_count 属性搜索(使用where 子句),但我收到错误,因为这不是我的数据库表中的真实列。
- 这就是我的搜索方式:
$organisations = Organisation::query();
$organisations = $organisations->where('device_count', '=', 0);
$organisations = $organisations->get();
- 这是错误:
[2020-10-14 12:29:27] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'device_count' in 'field list'...
是否有替代where 子句的替代方法,以便通过device_count 进行搜索?
【问题讨论】:
标签: php laravel search eloquent attributes