Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent

Hi Folks,

I have several models that contain a polymorphic field. These poly fields contain four or five relationships each, some are soft deleteable, some are not. I'm running into a 'damned if you do, damned if you don't' problem and not sure how to solve.

If I load with a call to eager loading without including trashed on the models that have softdeletes:

$deptRisks = \App\DeptRisk::with(['owner', 'riskable']);

I will get the following error on those models that have been soft-deleted: Trying to get property of non-object

If I load WITH the withTrashed for the softdeletes:

 $deptRisks = \App\DeptRisk::with(['owner', 'riskable' => function ($query) {
            $query->withTrashed();
    }]);

I will get the following error on those models that have NOT been set up as soft-deletes: Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

I'm sure I'm missing something obvious, but I'm spinning in circles. Please help.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.