$orders = Orders::findOrFail($id); // or just find
$orders->meals()->withTrashed()->get();
Something like that, adjust per your relations.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have orders, which are linked to meals. I normally retrieve these orders using:
$this->orders()->with('meals')->get()
After I try soft deleting a meal however, I tried to get it using the following:
$this->orders()->with('meals')->withTrashed()->get()//Did not work
$this->orders()->with('meals')->get() // And changed the meals function to read
public function meals(){
return $this->hasMany(Meal::class)->withTrashed();
}
//This also did not work
Everything I'm reading says this is the correct way to do it but the listed meal relation is always null. I checked the database and the meals table has the deleted_at column like it should. If I set that back to null, undeleting the item the relationship is restored and I am able to retrieve it.
What am I doing wrong here? How can I properly access this deleted relationship?
Edit: I should say I am encountering this problem on the order history page because I still want to show that they ordered it in the past. I am currently wrestling with not showing active orders that have their meals deleted, I think I need to add ->whereNull('meals.deleted_at') or something to my orders query, but that's a problem for another post.
Hi @bmcn99
In this case, as you are using Eager Loading, you should use the intended way to Constrain it.
I believe this should work:
$this->orders()->with(['meals' => function($query) {
$query->withTrashed();
}])->get()
ref: https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads
Hope it works for you!
Please or to participate in this conversation.