phplaraveleloquentilluminate-container

Why isn't this Laravel model deleting?


I'm just trying to make a simple form that deletes a model record from the database. I've done it before, but the following code only flashes the success message and redirects. The record never leaves the db.

Here is my form:

@foreach ($users as $user)
    <li class="list-group-item {{($loop->iteration % 2 == 0) ? 'even' : 'odd'}}">
        <h3>{{$user->first_name}} {{$user->last_name}}</h3>
        <b>AccessID: </b> {{$user->id}} <br />
        <b>Created: </b>  {{$user->updated_at}}
        <form action="{{ url('/users/delete', ['id' => $user->id]) }}" method="post">
            <input type="hidden" name="_method" value="delete" />
            {!! csrf_field() !!}
            <button type="submit">Delete</button>
        </form>
    </li>
@endforeach

Here is my controller method:

public function destroy(User $user)
{
    $user->delete();
    Session::flash('action', 'Deleted');
    Session::flash('status', $user->first_name . $user->last_name .'&#146;s access successfully removed.');
    return redirect('/users');
}

Here is my route: Route::delete('/users/delete/{id}', 'UserController@destroy');


Solution

  • The answer was that my route parameter needed to be named user for laravel to recognize that it should expect a user object. I had {id} instead of {user}