Search code examples
laravelrecords

How to bring related trashed records in Laravel 5.5


In my application I have notes which can be created by a user. User can also create folders and can move notes to folders. User can also trash folders and notes. I also have a trash can in my app.

When a user trashes his/her folder then every note in the folder is also trashed.

The problem is that when I show trashed folders in the "Trash Can" then trashed notes do not get retrieved with this code snippet.

if I go to db and manually make a note untrashed then i do get the note with trashed folders listing.

Here is my code like:

$trashed_folders = Folder::onlyTrashed()->where('user_id', $user->id)->with('note')->get();

Thanks in advance for the helpers


Solution

  • Try to eager load trashed notes as well.

    $trashed_folders = Folder::onlyTrashed()
        ->where('user_id', $user->id)
        ->with(['note' => function ($query) {
            $query->withTrashed();
        }])->get();