简体   繁体   English

Laravel 童子军关系问题

[英]Laravel Scout issue with relationship

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 :使用 Laravel scout 我想删除一条记录(在 Agent 模型上),一切正常,除了我在删除后重定向到代理列表,并且由于队列尚未处理,删除的代理从 Meilisearch 返回,但 Eloquent model 不再存在,导致错误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...令我惊讶的是,侦察员在集合中包含 null 个值......

However, you can run a ->filter() ( docs ) on the returned collection to remove all null results in it:但是,您可以在返回的集合上运行->filter() ( docs ) 以删除其中的所有null结果:

Agent::search($this->search)->get()->filter();

The collection now does not contain any null values该集合现在不包含任何 null 值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM