【发布时间】:2021-02-20 11:22:14
【问题描述】:
在 Laravel 范围内使用自定义属性
模型.php
public function getClientNameAttribute()
{
return $this->client->first_name . ' ' . $this->client->last_name;
}
public function scopeSearch($query, $search)
{
return $query->where('number', 'LIKE', '%' . $search . '%')
->orWhere('client_name', 'LIKE', '%' . $search . '%');
}
我收到一个错误SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_name' in 'where clause'
【问题讨论】:
-
您不能在查询中使用这些属性。自定义属性在 php 应用程序中,而不是在 mysql 数据库中
-
我还读到您不能在查询中使用属性。也许我会尝试另一种解决方案。谢谢!
-
这能回答你的问题吗? eloquent search/where on custom attributes
-
@PouyaHeydari 我遇到了那个解决方案,但它不能解决我的问题。但我已经使用
->whereHas('table', function ($query) {})解决了我的问题
标签: php laravel eloquent model