The error "Typesense\Exceptions\RequestMalformed: Parameter `fields` is required" typically means that when Scout (with Typesense driver) is creating a schema in Typesense, it's missing the `fields` definition for your searchable model.
This generally happens if your model's `toSearchableArray()` returns an empty array, or if there are issues determining which fields should be mapped.
**Here's how you can resolve this:**
1. **Check `toSearchableArray()` Implementation:**
In your Eloquent model (e.g., `App\Models\Post`), ensure you are returning all the searchable fields:
```php
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
// add all the fields you want to be indexed here
];
}
-
Check Data in Database:
If this happens right after
migrate:fresh, your database might be empty, so the first model retrieved by Scout returns an empty array whentoSearchableArray()is called.
To fix this, either:- Run your seeders after migration, before running
scout:import. - Or, manually add a test record to the relevant table.
Example:
php artisan migrate:fresh --seed php artisan scout:import "App\Models\Post" - Run your seeders after migration, before running
-
Scout Driver Configuration:
Make sure your
config/scout.phpand Typesense configuration are correct.
For the official Typesense Scout Driver, your model should have thesearchabletrait and correctly definetoSearchableArray. -
Clear Typesense Collections (optional):
If you changed the fields, you may need to delete existing collections in Typesense before importing again. You can do this via the Typesense dashboard or API.
Summary:
The error is usually caused by having an empty table (no data yet) after migration. The import command can't infer the Typesense fields from missing data. Seed your database before running scout:import, and double check your toSearchableArray() method.
Let me know if you have further questions!