Kevin
Kevin

Reputation: 43

delete method not allowed and returning MethodNotAllowedHttpException in Laravel 5

So I can create a new record on my database with form, but somehow i fail to delete my database using form And right now, I'm using laravel 5. So this is my code looks like

routes.php

Route::get('/', [
    'as' => '/', 
    'uses' => 'PagesController@getIndex'
]);

Route::resource('product', 'ProductController');

ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;

use App\Product;

use resources\views\products;

    class ProductController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
           $product = DB::select('select * from feedback');

            return view('products.index')
              ->with('product',$product);
        }

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

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
           /*$rating = new Product;
           $rating->Name= $request->name;
           $rating->avg=$request->price;
           $rating->save();*/

           $inputs= $request->all();
           $product= Product::create($inputs);

          //return redirect()->route('product.index');
           return redirect()->action('ProductController@index');
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {

            $product= Product::where('idoveralRating',$id)->first();

            //return $product;
            return view('products.show')
                ->with('product',$product);
        }

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

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

        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
             //echo '<script>console.log("bitch")</script>';
            //Product::destroy($id);
            $product= Product::where('idoveralRating',$id)
               ->delete();

            return redirect()->route('product.show');
        }
    }

show.blade.php

@extends('layouts.layout')
@section('Head')
    <h1>{{$product}}</h1>
@stop
@section('Body')
<h1>{{$product->Name}}</h1>
{!!Form::open([
    'method' => 'delete',
    'route'=> array('product.destroy',$product->id),

])!!}


{!!Form::submit('Delete')!!}
{!!Form::close()!!}
@stop

index.blade.php

@extends('layouts.layout')

@section('Head')
    ALL Product
@stop

@section('Body')

    @foreach($product as $col)
        <h1>{{$col->Name}}</h1>
    @endforeach

@stop

layout.blade.php

<!DOCTYPE html>
<html>
    <head>
    @yield('Head')
    </head>
    <body>

        @yield('Body')
    </body>
</html>

Ok, So im trying to delete my database based on my database id that i typed onmy browser link(so let say i type product/1),it means i want to delete my my database with id of 1.

What I've achieve so far is that I'm able to show my database based on id i typed but somehow when i want to route the id to my destroy method in ProductController class, it shows that method=>'delete' not allowed,what am i do wrong?

Upvotes: 1

Views: 6196

Answers (4)

Kevin
Kevin

Reputation: 43

Ok i want to answer my own question, the problem in my code is that I'm using a default primary key of which is equal to id(in other word, $primaryKey = 'id'), that's why when i pass my $id to destroy it seems wrong because I pass primary key that doesnt belong to the table, that's why the delete method seems not recognized because of you can't delete something that not exist.

so the problem is solved with simple step change the primary key,protected $primaryKey='idOveralRating'(for my case, and it works!)

Upvotes: 0

Paul Androschuk
Paul Androschuk

Reputation: 796

Try to use 'laravel data binding'. Add to your RouteServiceProvied.php in boot method following code:

$router->model('Product', Product::class);

And change destroy method at your controller to this:

public function destroy(Product $product)
{
    $product->delete();

    return back();
}

And your route at show.blade.php file must be like this:

'route'=> array('product.destroy', $product),

Upvotes: 3

Autista_z
Autista_z

Reputation: 2541

FORM dont have DELETE method.

You have to use it like this:

In your show.blade.php

{!!Form::open([
'method' => 'post',
'route'=> array('product.destroy',$product->id),])!!}
 {{ method_field('DELETE') }}
 ...

Upvotes: 1

huuuk
huuuk

Reputation: 4795

I think problem in your case not in delete route. After you delete record in destroy method, you return redirect to product.show route which is required parameter id.
So try

public function destroy($id)
{
    $product= Product::where('idoveralRating',$id)
       ->delete();

    return redirect()->route('product.index');

}

Upvotes: 0

Related Questions