问题描述
我有一个查询来获得所有软删除比赛:
I have a query to get all soft delete tournaments:
$tournaments = Tournament::onlyTrashed();
Thing是Tournament模型有一个FK owner_id。
Thing is Tournament model has a FK owner_id.
有时,所有者已被软删除,所以当我尝试获得$ tournament-> owner-> id时,我会收到一个例外。
Sometimes, owner has been soft deleted, so when I try to get $tournament->owner->id, I get an exception.
如何获取全部有没有一个比较优雅的(Eloquent)解决方案:
How to get All trashed Tournament which user is not soft deleted in Eloquent???
b
$tournaments = Tournament::onlyTrashed()
->join('users', 'users.id', '=', 'tournament.user_id')
->where('users.deleted_at', '=', null)
->select('tournament.*')
->get();
Tx!
推荐答案
有
和 whereHas
(对于更复杂的关系要求)可用于将结果限制为记录有一个积极的关系。
has
and whereHas
(for more complex relationship requirements) can be used to limit results to records that have an active relationship.
Tournament::has('Owner')
只会选择一个有效(现有和非软删除)所有者的比赛。
will select only tournaments with a valid (existing and non-soft-deleted) Owner.
这篇关于选择所有不被软删除的所有软删除的项目,在Eloquent / Laravel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!