lundi 26 juin 2017

Delete data from the database with Laravel 5.4

I am sort of new to the Laravel framework and I am building just a simple blog. I can create a blog, show a blog and show a overview of all blogs. Now I would like to delete a blog. So, I have created a delete button in my view with a route link which will pass also the id of the article. Then, in my routes file I specify a delete request and a controller method. In the method I find the id and try to delete the row with the id I specified in the route/view.

This doesn't work. Instead of activate the destroy/delete method it shows the article instead of deleting it and activates the show method instead of the delete method. Can somebody help me out, What do I wrong?

View.blade.php

<a href="" onclick="return confirm('Weet je dit zeker?')">
  <i class="fa fa-trash"></i>
</a>

Route

Route::group(['middleware' => 'auth'], function () {

    Route::get('/aanvragen', 'aanvragenController@index')->name('aanvragen.index');

    Route::get('/logout' , 'Auth\LoginController@logout')->name('logout');

    Route::get('/nieuws/toevoegen', 'blogController@create')->name('blogs.add');

    Route::post('/nieuws/store', 'blogController@store')->name('nieuws.store');

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

});

Route::get('/nieuws', 'blogController@index')->name('blogs.index');

Route::get('/nieuws/{blog}', 'blogController@show')->name('blogs.show');

Controller methods

Delete/Destroy

public function destroy($id) {

    $blog = Blog::find($id);

    $blog->delete();

    return redirect('/nieuws');

}

Show

public function show(Blog $blog) {

    dd('show');


    return view('blogs.show', compact('blog'));

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire