BelongsTo 字段上的可搜索方法在 laravel nova 中不起作用,不返回任何项目

searchable method on BelongsTo filed dose not work in laravel nova, not returning any items

我有一个名为 Customer 的模型,它与 Notes 模型有 HasMany 关系

public function notes()
{
    return $this->hasMany(Note::class);
}

NoteCustomer

BelongsTo 关系
public function customer()
{
   return $this->belongsTo(Customer::class);
}

然后我在 Note nova resource

中定义了相同的关系
BelongsTo::make('Customer', 'customer', Customer::class)

直到这里,如果想在 BelongsTo 字段上调用 ​​->searchable(),现在一切都可以正常工作,它不会 return 来自搜索的任何东西

BelongsTo::make('Customer', 'customer', Customer::class)->searchable()

我该如何解决这个问题

您调用 srearchable() 的字段必须在您资源的搜索数组中。

因此,如果您的字段是 BelogsTo,则必须将您所属资源的 title 字段放入 sreach 数组

在我的例子中,标题字段是

public static $title = 'name';

所以我把它放在

public static $search = [
    'id',
    'name',
];

一切都按预期进行

您的 BelongsTo 字段在不同的 table 中,因此如果您想在 Note 资源中搜索某些内容 Customer 字段您的搜索将不起作用。在这种情况下,我使用 SearchesRelations 包进行搜索。您可以安装此软件包并将此代码放入您的 Resource class

  public static $searchRelations = [
    'customer' => ['name', 'another_customer_field'],
];

请查看文档以获取更多详细信息SearchesRelations