S.M_Emamian
S.M_Emamian

Reputation: 17393

MethodNotAllowedHttpException - put and delete

I want to delete an item but I got this error message:

 (1/1) MethodNotAllowedHttpException

in RouteCollection.php line 255
at RouteCollection->methodNotAllowed(array('PUT', 'DELETE'))

my routes:

Route::group(['prefix' => '/Seller', 'middleware' => ['seller_access']], function ()  {

    Route::get('/','Seller\SellerController@index')->name('seller');

    Route::group(['prefix' => '/Products'], function ()  {
        Route::get('/', 'MarketingBundle\Seller\Product\ProductController@index')->name('marketing.seller.product.index');
        Route::delete('/{id}', 'MarketingBundle\Seller\Product\ProductController@delete')->name('marketing.seller.product.delete');
        Route::put('/{id}', 'MarketingBundle\Seller\Product\ProductController@update')->name('marketing.seller.product.update');
    });

my url:

Seller/Products/228

my controller:

class ProductController extends Controller
{

    public $resources = "marketing.seller.product";


    public function index(Request $request)
    {

        $products = \Auth::user()->sellerProduct()->paginate(10);
        return view($this->resources . '.index', [
            'products' => $products
        ]);
    }

    /**
     * @param $product_id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($product_id)
    {
        dd("masoud");
        \Auth::user()->sellerProduct()->detach(['product_id' => $product_id]);
        return response()->json(['status' => true]);
    }

Upvotes: 0

Views: 121

Answers (2)

Coen
Coen

Reputation: 73

In order for laravel to know you're sending a patch or delete request you need to add a method field into your forms.

<form method='POST' action='#'>
@csrf
{{ method_field('PATCH') }} 
</form>

Documentation https://laravel.com/docs/5.7/helpers#method-method-field https://laravel.com/docs/5.0/routing#method-spoofing

Upvotes: 0

Hitori
Hitori

Reputation: 589

re-order your routes like this.

Route::delete('/{id}', 'MarketingBundle\Seller\Product\ProductController@delete')->name('marketing.seller.product.delete');
Route::put('/{id}', 'MarketingBundle\Seller\Product\ProductController@update')->name('marketing.seller.product.update');
Route::get('/', 'MarketingBundle\Seller\Product\ProductController@index')->name('marketing.seller.product.index');

Upvotes: 1

Related Questions