首页 > 解决方案 > 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()

我怎么解决这个问题

标签: phplaravellaravel-nova

解决方案


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

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

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


推荐阅读