Using Laravel scout I want to delete a record (on an Agent model), everything works well except that I'm redirecting to a list of agents after the delete, and since the queue is yet to be processed the deleted agent comes back from Meilisearch, but the Eloquent model doesn't exist anymore, resulting in an error Attempt to read property "id" on null
:
// getting agents to display in the view
Agent::search($this->search)
->orderBy('id', 'asc')
->paginate($this->perPage)
// showing an agent ID in a loop in the view
{{ $agent->user->id }}
If reloading the page the list is fine - the deleted user has successfully been deleted. How can I prevent this error?
It surprises me that scout contains the null values in the collection...
However, you can run a ->filter()
(docs) on the returned collection to remove all null
results in it:
Agent::search($this->search)->get()->filter();
The collection now does not contain any null values