spacedog4
2 weeks ago
158
4
Laravel

SoftDelete on polymorphic relationships

Posted 2 weeks ago by spacedog4

I'm building a activity feed in my project, but I also want to record delete activity, the problem is, to do this, I need to use softDelete in some models, in others I don't want to, so how do I user "withTrashed" in a polymorthic relationship where sometimes it can be trashed and in other can't

In laravel docs, i saw that I can use the method "trashed" to check if the model is using softDelete, but when I do it in the with method I got this error: call to undefined method Illuminate\Database\Eloquent\Builder::trashed()

Here is what i'm doing now and not working

        return Activity::with([
            'subject' => function ($query) {
                if ($query->trashed()) {
                    return $query->withTrashed();
                }

                return $query;
            },
            'usuario'
        ])->latest()->take(50)->get()->groupBy(function ($activity) {
            return $activity->created_at->format('d/m/Y');
        });

Please sign in or create an account to participate in this conversation.