Amartya's avatar

destroy method in resource controller in laravel 7

Hello community, As I am learning laravel I am unable to understand that when I am trying to access my delete.blade.php page by typing posts/delete in URL(that file is in resource/views/posts) it is calling the method show and printing "show" on that page. please guide me what I am doing wrong here

delete.blade.php

@extends('main')

@section('content')
<form method="POST" action="{{route('posts.destroy', '$post->id') }}"  >
@method('DELETE')
@csrf
    <select name="id">
        <option value="1">vddv</option>
        <option value="2">miss</option>
        <option value="3">miss</option>
        <option value="4">joy</option>
    </select>

      <br><br>
    <button type="submit"> Delete blog</button>
</form>
@endsection

PostController.php (a resource controller)

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions; 


class PostController extends Controller
{ 
    public function create()
    {
        return view('posts.create');   
    }

    public function store(Request $request)
    {
       $post = new posts;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->save();
        return redirect('posts/read');
    }

    public function show($data)
    {
       echo "show";
    }

    public function edit($id)
    {
        return view('posts.edit');
    }

    public function update(Request $req, $id)
    {
        echo posts::where('title' , $req->title)
        ->update(['body'=>$req->body]);
        return redirect('/');
    }

    public function destroy($id)
    {
        $post = posts::find($id);
        $post->delete();
        return redirect('/');

    }
}

route:

Route::resource('posts', 'PostController');
0 likes
5 replies
Sinnbeck's avatar

The route needs to be called with a DELETE header. If you open an url in the browser it will always be GET. It there for finds the first route that matches GET, which is the show route :)

Instead use that form you created

Amartya's avatar

thanks, I did understand that get request part and why it is only calling show method sorry but I didn’t understand " route needs to be called with a DELETE header " :(

Sinnbeck's avatar

Well html only supports GET and POST in forms. Therefor you the helper @method('DELETE')to tell laravel to handle the POST request as a DELETE request. But if you were using a javascript library or a program like Postman, you could specifically send the request as a DELETE request.

Here is an example from postman http://i.imgur.com/bqbw7P7.png

siangboon's avatar

what Sinnbeck had explained was that the request method of browser is "GET" when you typed an url and hit the enter key, this is the default and the only behavior of a browser as the header already preset, and regardless of what method you wrote in the form as these two actions are different...

You need to submit a form and specify the "method" then only the "method" will injected into the request header, alternative is use some tool like Postman then you can select the method as you like...

Please or to participate in this conversation.