0

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');

Andrii Lutskevych
  • 1,349
  • 13
  • 23
KinsDotNet
  • 1,500
  • 5
  • 25
  • 53
  • 2
    Do you have soft deletion turned on? If you do, the `deleted_at` column will be filled in with a date, and that's all `delete()` does for that model. – ceejayoz Sep 21 '18 at 01:59
  • Or do you have a deleting Model event, which returns false? – fubar Sep 21 '18 at 02:02
  • I think you passed id not the entire object, so you need to get user object from id then use $user->delete(); – ankit patel Sep 21 '18 at 02:05

1 Answers1

1

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}

KinsDotNet
  • 1,500
  • 5
  • 25
  • 53