此路由不支持 GET 方法。支持的方法:PATCH、DELETE

The GET method is not supported for this route. Supported methods: PATCH, DELETE

我正在尝试向我的应用程序添加一个删除功能,其中将显示一个库存列表,您可以根据需要删除一个项目。但是,我不知道哪里出错了,因为它确实说支持删除。

这是我的路由器:

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

这是我的控制器:

<?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(): \Illuminate\Contracts\View\View
    {
        $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(): \Illuminate\Contracts\View\View
    {
        return view('pages.inventories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Redirector
     */
    public function store(Request $request): Redirector
    {
        $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): \Illuminate\Contracts\View\View
    {
        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): RedirectResponse
    {
        $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): RedirectResponse
    {
        $inventory = Inventory::find($inventory);
        $inventory->delete();
        return redirect()->route('inventories.index')->with('status',
            'Item has been deleted!' . $inventory->title);
    }
}

这是我的 delete.blade 文件:

@extends('layouts.app')

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

@section('content')
    <h1><strong>Delete inventory</strong></h1>
    <x-inventory-form :inventory=$inventory />
    {{ $inventory }}
    <form method="POST" action="{{url('/inventories',[$inventory->id])}}"></form>
    <input type="hidden" name="_method" value="delete">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
@endsection

这是我的 inventories.blade 文件:

@extends('layouts.app')

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

@section('content')
    <h1>Inventory Table</h1>
    <p>This is the inventory table made using PHP Laravel that is randomly generated.</p>
   <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Description</th>
                <th>Price</th>
                <th>In stock</th>
                <th>On sale</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            @foreach($inventories as $inventory)
            <tr>
                <td>{{$inventory->id}}</td>
                <td>{{$inventory->title}}</td>
                <td>{{$inventory->description}}</td>
                <td> &pound;{{ number_format($inventory->price, 2) }}</td>
                <td>{{$inventory->in_stock}}</td>
                <td>{{ $inventory->on_sale ? 'Yes' : 'No' }}</td>
                <td><a href="{{ route('inventories.edit', $inventory) }}">Edit</a></td>
                <td><a href="{{ route('inventories.destroy', $inventory) }}">Delete</a></td>
            </tr>
            @endforeach
        </tbody>
    </table>
@endsection

这是我的库存表格组件:

<?php

namespace App\View\Components;

use App\Models\Inventory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class InventoryForm extends Component
{
    /**
     * @var Inventory|null
     */
    public $inventory;

    /**
     * @param Inventory|null $inventory
     */
    public function __construct(Inventory $inventory = null)
    {
        $this->inventory = $inventory;
    }

    /**
     * @return string
     */
    public function action(): string
    {
        if ($this->inventory) {
            return route('inventories.update', ['inventory' => $this->inventory]);
        }

        return route('inventories.store');
    }

    /**
     * Get the view / contents that represent the component.
     *
     *
     * @return View|\Closure|string
     */
    public function render()
    {
        return view('components.inventory-form');
    }

这是我的库存表单组件 blade 文件:

<form action="{{ $action }}" method="post">
    @if($inventory)
        @method('patch')
    @endif
    @csrf
    <label for="title">Enter an item name:</label>
    <input type="text" name="title" value="{{ $inventory?->title }}" required /><br><br>

    <label for="description">Enter the item's description:</label>
    <textarea name="description" required>{{ $inventory?->description }}</textarea><br><br>

    <label for="price">Enter the item's price:</label>
    <input type="number" name="price" value="{{ $inventory?->price }}" required/><br><br>

    <label for="in_stock">Enter a number of items in stock:</label>
    <input type="number" name="in_stock" value="{{ $inventory?->in_stock }}" required/><br><br>

    <label for="on_sale">Select yes/no if item is on sale:</label>
    <select name="on_sale" value="{{ (int) $inventory?->on_sale }}" required>
        <option value="" disabled {{ $inventory ? '' : 'selected' }}>--Please choose an option--</option>
        <option value="1" {{ $inventory?->on_sale ? 'selected' : '' }}>Yes</option>
        <option value="0" {{ (false === $inventory?->on_sale) ? 'selected' : '' }}>No</option>
    </select><br><br>
    <button type="submit">Submit</button>
</form>

如有任何帮助,我们将不胜感激!!

要更改表单中使用的方法,要使用的属性是 _method 而不是 __method_token

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

你的名字错了__method应该是_method而已。

您可以改用 blade 指令。

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

将此用于删除方法。

@method('DELETE')
@csrf

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

如果您正在使用 laravel 5。*

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

你错过了

<form method="POST" action="{{url('/inventories',[$inventory->id])}}"></form>

所以它选择当前 url 作为 get 方法。所以添加应该修复它。

@extends('layouts.app')

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

@section('content')
    <h1><strong>Delete inventory</strong></h1>
    {{ $inventory }}
    <form method="POST" action="{{url('/inventories',[$inventory->id])}}"> 
        @method('delete')
        @csrf
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    </form>
@endsection

在我的路由文件中,我需要编写一个带删除的获取请求和一个带销毁的删除请求。我新编辑的代码是:

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

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

还有我的控制器:

 /**
     * @param Inventory $inventory
     * @return \Illuminate\Contracts\View\View
     */
    public function delete(Inventory $inventory)
    {
        return view('pages.inventories.delete', ["inventory" => $inventory]);
    }

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