jordano1
2022
13
Laravel

Route Model binding with soft deleted model

Posted 1 year ago by jordano1

Route Model binding with soft deleted model home.blade file:

<form method="POST" action="/restore/{{$trashed_user->id}}/restoreUser">
{{ csrf_field() }}
<button type="submit" class="btn btn-info">Restore User</button>
</form>

routes/web.php file:

Route::get('/restore/{user}/restoreUser', '[email protected]');
Route::post('/restore/{user}/restoreUser', '[email protected]');

HomeController

    public function restore(User $user)
    {    
        $user->restore();   
        return view('restore.restoreUser');
    }

Few questions here: Q1: do I need a csrf_field on each of these forms? Q2: Can you actually do route model binding with trashed users? I've seen people talk about {bind} but I'm not really sure how that works, I kinda get lost with it.

Things I have tried: changing the view's form action to /restore/{user}/restoreUser, and gone through the process in getting the correct ID, but the only way I can actually get the ID is by making a variable called "trashed_users" but I'm not sure how I can get the restore function working.

If you need to see my index function, where the trashed_users variable is stored, it's right here:

   public function index()
    {
        //storing all users from the database as $user
        $users = User::all();
        //storing all trashed users in the trashed_users var
        $trashed_users = User::onlyTrashed()->get();
        //return home with a users array and trashed users array for the foreach loops on the homepage
        return view('home', compact('users', 'trashed_users'));
    }

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