How would I get a list of active Users plus a User that might be soft-deleted? Say, a salesperson is attached to a Sale. That salesperson might leave, but if I pull up a Sale form months later, I still want that person in the form dropdown list. If I remove SoftDeletes from Users, the query would be User::whereNotNull('deleted_at')->orWhere('id', $sale->salesperson_id)
You can use the withTrashed method.
https://laravel.com/docs/8.x/eloquent#querying-soft-deleted-models
withTrashed returns all soft deleted users. I want all active users, plus the one that's (possibly) soft deleted. This works:
public function scopeActiveOr($query, $id){
return $query->withTrashed()->where(function($q) use($id){
$q->whereNull('deleted_at')
->orWhere('id', $id);
});
}
But I'd like to not have to apply SoftDeletes, remove them, then reapply them. I thought there'd be a native solution.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community