When called the update method, the Laravel does not recognize the route

Asked

Viewed 607 times

-2

I’m beginner with Laravel(5.5) and I have a problem. I created a crud of products, can list it and view the record, but when I try to do an update, it returns an error:

Methodnotallowedhttpexception

BLADE:

@extends('base')
@section('main')
<div class="row">
    <div class="col-sm-8 offset-sm-2">
        <h1 class="display-3">Update a product</h1>

        @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        <br />
        @endif
        <form method="post" action="{{ route('products.update', $product->id) }}">
            @method('PATCH')
            @csrf
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" name="name" value={{ $product->name }} />
            </div>

            <div class="form-group">

                <label for="description">Description:</label>
                <input type="text" class="form-control" name="description" value={{ $product->description }} />
            </div>


            <div class="form-group">
                <label for="quantity">Quantity:</label>
                <input type="text" class="form-control" name="quantity" value={{ $product->quantity }} />
            </div>

            <div class="form-group">
                <label for="price">Price:</label>
                <input type="text" class="form-control" name="price" value={{ $product->price }} />
            </div>

            <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>
@endsection

CONTOLLER:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProductRequest;
use App\Product;

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::orderBy('created_at', 'desc')->paginate(10);
        return view('products.index',['products' => $products]);
    }

    public function create()
    {
        return view('products.create');
    }

    public function store(ProductRequest $request)
    {
        $product = new Product;
        $product->name        = $request->name;
        $product->description = $request->description;
        $product->quantity    = $request->quantity;
        $product->price       = $request->price;
        $product->save();
        return redirect()->route('products.index')->with('message', 'Product created successfully!');
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $product = Product::findOrFail($id);
        return view('products.edit',compact('product'));
    }

    public function update(ProductRequest $request, $id)
    {
        $product = Product::findOrFail($id);
        $product->name        = $request->name;
        $product->description = $request->description;
        $product->quantity    = $request->quantity;
        $product->price       = $request->price;
        $product->save();
        return redirect()->route('products.index')->with('message', 'Product updated successfully!');
    }

    public function destroy($id)
    {
        $product = Product::findOrFail($id);
        $product->delete();
        return redirect()->route('products.index')->with('alert-success','Product hasbeen deleted!');
    }
}

ROUTES

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

// Route::resource('products', 'ProductController')->middleware('auth');
Route::resource('products', 'ProductController');
  • Searching found that it might have something to do with @csrf. I also tried to use {{ csrf_field() }}

  • Dude, you already tried to get this guy out of Lade @method('PATCH'), the form has the method and I think the Fixable can understand the update method without it.

1 answer

0

Operation of the Laravel Cruds

The exception "Methodnotallowedhttpexception" is triggered when an invocation is made for a path whose method (POST, GET, PUT/PATH, DELETE) has not been specified correctly.

For example, suppose you declare a route like this:

Route::get('products', 'ProductController');

If you invoke her with form method="post" action="{{ route('products') }}", the exception will be fired as there is no route POST for "products".

Your problem

According to the documentation of Laravel 5.5, it is necessary to use PATCH or PUT to invoke a ". update" route in a CRUD.

For this to be done, there are two ways:

  1. Create a field in the form input type="Hidden" name="_method" value="PATCH";
  2. Use a helper for this, in case {{ method_field('PATCH') }}.

The problem is that you are using the @method('PATCH') Blade directive.

But the @method directive is only available on Laravel 5.6 or higher.

Since your version is 5.5, then you should use the helper {{ method_field('PATCH') }} in place.

<form method="post" action="{{ route('products.update', $product->id) }}">

    {{ method_field('PATCH') }}
    
    @csrf
    
    <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" class="form-control" name="name" value={{ $product->name }} />
    </div>
    
    ....

  • Great, I did exactly, and it passed the route, but somehow it doesn’t find the request: it follows the error: Reflectionexception Class App Http Requests Productrequest does not exist. The same is true when trying to create. When trying to remove the record, it works normally

  • The firing of Methodnotallowedhttpexception has been resolved. This is another problem... try to review the location of the Productrequest class and whether the namespace within it is correct. Reflection issues mean Laravel didn’t find the class you specified in Controller.

  • For some reason this class was not really created, now it solved. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.