Home > Back-end >  The GET method is not supported for this route. Supported methods: PATCH, DELETE
The GET method is not supported for this route. Supported methods: PATCH, DELETE

Time:01-05

I am trying to add a delete function to my application where there will be a list of inventory presented and you can delete an item if you wish. However, I don't know where I am going wrong since it does say delete is supported.

Here my my router:

Route::delete('/inventories/{inventory}', [\App\Http\Controllers\InventoryController::class, 'destroy'])->name('inventories.destroy');

Here is my controller:

<?php

namespace App\Http\Controllers;

use App\Models\Inventory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Log;

class InventoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function index()
    {
        $inventories = Inventory::all();
        return view('pages.inventories',[
            "inventories" => $inventories
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function create()
    {
        return view('pages.inventories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Redirector
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title'=> 'required|string',
            'description'=> 'required|string|max:300',
            'price' => 'required|integer|min:0',
            'in_stock' => 'required|integer',
            'on_sale' => 'required|boolean'
        ]);
        $inventory = new Inventory();

        $inventory->fill($validated)->save();

        return redirect('/inventories');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function edit(Inventory $inventory)
    {
        return view('pages.inventories.edit',[
            "inventory" => $inventory
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Inventory $inventory
     * @return RedirectResponse
     * @throws ValidationException
     */
    public function update(Request $request, Inventory $inventory)
    {
        $validated = $this->validate($request, [
            'title'=> 'required|string',
            'description'=> 'required|string|max:300',
            'price' => 'required|integer|min:0',
            'in_stock' => 'required|integer',
            'on_sale' => 'required|boolean'
        ]);
        $inventory->fill($validated)->save();
        return redirect()->route('inventories.index')->with('status',
            'Item has been updated!' . $inventory->title);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Inventory $inventory
     * @return RedirectResponse
     */
    public function destroy(Inventory $inventory)
    {
        $inventory->delete();
        return redirect()->route('inventories.destroy')->with('Item has been deleted!');
    }

Here is my delete.blade file:

@extends('layouts.app')

@section('title', 'Delete Inventory')

@section('content')
    <h1><strong>Delete inventory</strong></h1>
    {{ $inventory }}
        <input type="hidden" name="__method" value="delete">
        <input type="hidden" name="__token" value="{{ csrf_token() }}">
    @if ($errors->any())
        <div >
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
@endsection

Any help would be appreciated!!

CodePudding user response:

Your name is wrong __method it should _method only.

You may use blade directive instead.

@section('content')
<h1><strong>Delete inventory</strong></h1>
{{ $inventory }}
    @method('DELETE')
    @csrf
@if ($errors->any())
    <div >
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@endsection

CodePudding user response:

Use this for delete method.

@method('DELETE')
@csrf

OR

<input type="hidden" name="_method" value="delete" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">

If you are using laravel 5.*

{!! method_field('delete') !!}
{!! csrf_field() !!}

CodePudding user response:

To change the method used in a form, the attribute to use is _method not __method. Same thing for _token

<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
  •  Tags:  
  • Related