3
votes

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="{{route('nieuws.destroy', ['id' => $blog->id])}}" 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'));

}
4
@Gijsberts you might want to re-check kunal's answer. Allowing GET requests to change data in any way is bad practice and a security risk.Robert

4 Answers

3
votes

A delete() route requires you to POST your data.

HTML forms only supports GET and POST, other methods like DELETE, PUT, etc are not supported, that's why Laravel uses the _method to spoof methods which are not supported by HTML forms.

You do not want use GET in these cases, since someone can send a user the url (http://yoursite.com/blog/delete/1) in an IM or via email. The user clicks and the blog is gone.

Define your route as it would be when using resource controllers, so:

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

And either use a form with the delete method:

// apply some inline form styles
<form method="POST" action="{{ route('nieuws.destroy', [$blog->id]) }}">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">Delete</button>
</form>

Or do some javascript magic as the link SR_ posted in his comment on your OP.


One more thing, add some sort of validation in your destroy action. Right now when you provide a non-existing id or something else, you will get a 500 error, instead you want to have a 404.

public function destroy($id)
{
    $blog = Blog::findOrFail($id);

    $blog->delete();

    return redirect('/nieuws');
}
1
votes

I think you need to update your destroy function like:

public function destroy($id) {

    $blog = DB::table('blog')->where('id',$id)->delete();

    return redirect('/nieuws');

}

And update your view code like:

<a href="{{route('nieuws.destroy', [$blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
  <i class="fa fa-trash"></i>
</a>

Hope this work for you!

0
votes

I'm also new to Laravel but I made it work through this way: (I use 'Article' as the model's name and the resource "method" in the route stands for a bunch of useful routes including the route you wrote)

Controller:

public function destroy($id){
        Article::find($id)->delete();
        //$article = Article::find($id);
        return redirect()->back()->withErrors('Successfully deleted!');
    }

Route:

Route::resource('article','ArticleController');

However, I think the problem lies in the default definition of database's name of your model. Laravel will assume that you have a database named blogs since you have a model named "blog". Are you having the database's name right?

0
votes

To use DELETE HTTP Verb, your form should consists of the POST method and settings the method_field('DELETE')

Example:

<form method="POST" action="{{ route('xxx.destroy', $xxx->id) }}">
    {{ csrf_field }}
    {{ method_field('DELETE') }}
</form>