1

Пытаюсь сделать что то вроде todo листа, когда пробую сделать удаление, выходит эта ошибка "The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST" Пожалуйста подскажите в чем может быть ошибка, или что проверить и куда копать

index.blade.php

@extends('layouts.app')

@section('title', 'Pupil Page')


@section('content')

@if ($message = Session::get('success'))
<div class="alert alert-success ">
    {{ session()->get('success') }}
</div>
@endif

<div class="d-flex justify-content-start">
    <a href ="{{ route('pupils.create') }}" class="btn btn-primary" type="submit">Добавить ученика</a>
</div>
<div>
    <table class="table mt-3 ">
        <thead class="thead-dark">
            <tr>
                <th scope="col">Фамилия</th>
                <th scope="col">Имя</th>
                <th scope="col">Отчество</th>
                <th scope="col">Дата Рождения</th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            @foreach($pupils as $pupil)
            <tr>
                <td>{{$pupil->surname}}</td>
                <td>{{$pupil->name}}</td>
                <td>{{$pupil->lastname}}</td>
                <td>{{$pupil->birthday}}</td>
                <td class = "table-buttons">

                    <a href = "{{route('pupils.edit', $pupil)}}" class="btn btn-primary">
                      <i class="fa fa-pencil"></i>
                    </a>
                    <form method = "POST" action = "{{ route('pupils.destroy', $pupil) }}">
                        @csrf
                        @method('DELETE')
                        <button type="submit">delete</button>
                    </form>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
@endsection

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::resource('pupils', 'PupilsController');

PupilsController.php

<?php

namespace App\Http\Controllers;

use App\Pupils;
use Illuminate\Http\Request;

class PupilsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pupils = Pupils::all();
        return view('pupils.index', compact('pupils'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('pupils.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'surname' => 'required|max:255',
            'name' => 'required|max:255',
            'lastname' => 'required|max:255',
            'birthday' => 'required|max:255',
            'address' => 'required|max:255',
        ]);

        $pupils = new Pupils(
            [
                'surname' => $request->get('surname'),
                'name' => $request->get('name'),
                'lastname' => $request->get('lastname'),
                'birthday' => $request->get('birthday'),
                'address' => $request->get('address'),
            ]
        );

        $pupils->save();
        return redirect('/pupils')->with('success', 'Ученик добавлен!');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Pupils  $pupils
     * @return \Illuminate\Http\Response
     */
    public function show(Pupils $pupils)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Pupils  $pupils
     * @return \Illuminate\Http\Response
     */
    public function edit(Pupils $pupils)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Pupils  $pupils
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Pupils $pupils)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Pupils  $pupils
     * @return \Illuminate\Http\Response
     */
    public function destroy(Pupils $pupils)
    {

        $pupils->delete();

        return redirect('/pupils')->with('success', 'Ученик удален!');
    }
}

так же прилагаю route list

5
  • 1
    Гуглить пробовали ? 1) https://stackoverflow.com/questions/56293326/the-delete-method-is-not-supported-for-this-route-supported-methods-get-head, 2) https://laracasts.com/discuss/channels/laravel/the-delete-method-is-not-supported-for-this-route-supported-methods-get-head-post и т.д. (много в поиске) 31 дек 2021 в 2:30
  • @sousage1212 пробовал делать, то что там указано но не помогает, поэтому и написал сюда. 31 дек 2021 в 7:55
  • Вы вызываете путь, который не поддерживает DELETE, посмотрите на ваш скрин и скажите, какой путь вы вызываете. 31 дек 2021 в 9:09
  • @Daniel Protopopov вы имеете ввиду сделать так ? Route::delete('pupils', 'PupilsController@destroy')->name('pupils.destroy'); 31 дек 2021 в 10:41
  • Вы передаете объект шаблонизатору, который не знает как его использовать чтобы сгенерировать путь. Вам нужно поменять route('pupils.destroy', $pupil) на route('pupils.destroy', $pupil->id) чтобы получать ID объекта, который затем через внутренние механизмы будет превращен в объект. 1 янв 2022 в 11:16

1 ответ 1

1

Добавьте в web.php

Route::get('pupils/{pupil}', [App\Http\Controllers\PupilsController ::class, 'destroy'])->name('pupils.destroy');

В контролере метод

    public function destroy($pupils)
{
    Client::destroy($pupils);

    return back()->with('success', 'Ученик успешно удален!');
}

В view добавь

<a href = "{{route('pupils.destroy', $pupil)}}" class="btn btn-primary">Delete</a>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.