Search code examples
phplaravellaravel-nova

Searchable method on BelongsTo field does not work in Laravel Nova, not returning any items


i have a model called Customer that has HasMany Relatinship toNotes model

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

and Note has BelongsTo relationship to Customer

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

then i defined the same relationship in Note nova resource

BelongsTo::make('Customer', 'customer', Customer::class)

until here every thing works perfectly now if want to call ->searchable() on BelongsTo field it doesn't return any thing from search

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

how can i solve this problem


Solution

  • the field that you call srearchable() on must be in search array in your resource.

    so if your field is BelogsTo you must put the title field of the resource that you are belonging to into sreach array

    in my case the title field is

    public static $title = 'name';
    

    so i put it in

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

    and every thing works as expected