Skip to content

Instantly share code, notes, and snippets.

@joydeep-bhowmik
Last active May 16, 2024 09:56
Show Gist options
  • Save joydeep-bhowmik/00d676416666efb770690aa89bac75df to your computer and use it in GitHub Desktop.
Save joydeep-bhowmik/00d676416666efb770690aa89bac75df to your computer and use it in GitHub Desktop.
Laravel Egarloading with scout search

Laravel Egarloading with scout search

Suppose this is your category model

class Category extends Model
{
    use HasFactory, Searchable;

    public function toSearchableArray(): array
    {
        return ['name' => $this->type];
    }

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_category_id');
    }
}

Then you can load relations by the following way

 $categories = tap(Category::search($request->key)->paginate($request->perPage ?? 10), function ($categories) {

            return $categories->load('parent');
        });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment