php - BelongsTo 上的可搜索方法在 laravel nova 中不起作用,不返回任何项目
问题描述
我有一个名为Customer的模型,它与Notes模型有HasMany Relatinship
public function notes()
{
return $this->hasMany(Note::class);
}
并且Note与Customer具有BelongsTo关系
public function customer()
{
return $this->belongsTo(Customer::class);
}
然后我在Note nova 资源中定义了相同的关系
BelongsTo::make('Customer', 'customer', Customer::class)
直到这里,如果想调用->searchable()
BelongsTo 字段,现在一切都可以正常工作,它不会从搜索中返回任何东西
BelongsTo::make('Customer', 'customer', Customer::class)->searchable()
我怎么解决这个问题
解决方案
您的BelongsTo字段位于不同的表中,因此如果您想在Note Resource with Customer字段中搜索某些内容,您的搜索将不起作用。在这种情况下,我使用SearchesRelations包进行搜索。您可以安装此软件包并将此代码放入您的Resource类中
public static $searchRelations = [
'customer' => ['name', 'another_customer_field'],
];
请查看文档以获取更多详细信息SearchesRelations。
推荐阅读
- javascript - createObjectURL 返回未定义
- google-drive-api - Google API 中是否存在“获取我所在的 Google 群组”端点?
- java - 在代码样式中增加行长后展开行
- javascript - .then() 试图在承诺解决之前运行
- java - 使用 'mvn spring-boot:run' 时出错;但不适用于 java -jar 或 intelliJ
- sql - 将列乘以另一个表中的行值
- jquery - 如何使用 jquery 解析句子 json?
- python - 将具有不同结构的表附加/合并到 Hive 中的一个表中?
- javascript - 将数组元素传递给 foreach div
- gtk - 单击按钮后添加元素