BlackPearl
BlackPearl

Reputation: 2775

Laravel destroy won't delete data from database

Trying to delete an entry from database using the following in Laravel 5.4 without success.

My View

{!! Form::open(['method' => 'DELETE', 'route' => ['faculty.delete', $faculty->id]]) !!}
    <button type="submit" class="btn btn-danger btn-sm">Delete</button>
{!! Form::close() !!}

My Route

Route::delete('faculty/delete/{id}', 'FacultyController@destroy')->name('faculty.delete');//delete faculty

My Controller

public function destroy(Faculty $faculty)
{
    //Log::info($faculty);
    $faculty->delete();
    return redirect('faculties/faculty');
}

The log in the controller returns an empty value when I allow it to run.

The problem is that the code does not delete the item from the table.

Upvotes: 0

Views: 595

Answers (1)

reinierkors
reinierkors

Reputation: 510

You need to change faculty/delete/{id} to faculty/delete/{faculty}.

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

Since the $faculty variable is type-hinted as the App\Faculty Eloquent model and the variable name matches the {faculty} URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.

https://laravel.com/docs/5.4/routing#route-model-binding

Upvotes: 1

Related Questions