Is there a way to use withTrashed
with relationships in Eloquent.
What I need is this. I have table and model Mark
and another table User
. User
has many Mark
and Mark
belongs to User
. So I defined this in Eloquent models.
Now I need to get an instance of Mark
that is soft deleted. This is not a problem if User
isn't soft deleted, but if both Mark
and User
are soft deleted, I get an error Trying to get property of non-object
, because
$mark->user
won't return actual user, cause it is soft deleted.
Is there a way that I can do something like
$mark->withTrashed()->user
to get this related user even if it is deleted?
Depending on your needs, you can define the relationship:
or use it on the fly:
then your case would turn into:
You can do that like this: