2
votes

i'm new to laravel, and i found few decent tutorials to help me understand and get started with it.
the problem is-> whenever i want to use the post method this exception raises MethodNotAllowedHttpException
but unlike, maybe 99% of who asked similar questions, in my case it says the exception is in RouteCollection.php line 218, which is unusual but not to laravel 5.2.x

the following is the methode post in routes.php:

Route::post('/ajouter_produit', 
[
    'uses'=>'ProductController@addProduct',
    'as'=>'ajouter_produit',
]);

i even tried adding this method to a middleware route group but the problem remained.

this is my controller:

public function addProduct (Request $request)
{

    $this->validate($request, [
        'label'=>'required|alpha',
        'prix'=>'required|numeric',
    ]);

    $prod = new Product();
    $prod->label=$request['label'];
    $prod->type=$request['type'];
    $prod->prix=$request['prix'];

    $prod->save();

    return view('welcome');

}

and this is my form:

<form action="{{ route('ajouter_produit') }}" method="post" >

    <input type="text" name="label" id="label"/>

    <select name="type" id="type">
        <option value="1">Par unité</option>
        <option value="2" selected>Par kilo</option>
    </select>

    <input type="text" name="prix" id="prix"/>

    <button type="submit">Ajouter</button>

    <input type="hidden" value="{{ Session::token() }}" name="_token"/>

i also tried this but it raised the same problem:

Route::post('/trypost', function () {
    return 'hello post';
});

can you please help me !!

if you need any other source just ask for it.
Every effort will be much appreciated. thank you

2
Missing method get. First you should create route with method get return view. Next, in view page you call method post validate and save to your model.mydo47
You can verify your route using command php artisan route:listGanesh Ghalame
already tried that.. didn't workLandoulsy Med
Check page view blade. Error endifmydo47
OMG thank youuu @mydo47 it worked!! i can't believe i didn't think of that!! thank youuuLandoulsy Med

2 Answers

1
votes

take note that if you are using route(), it is expecting route name, such as user.store or user.update.

so my suggestion is, try use url() for your open form

<form action="{{ url('ajouter_produit') }}" method="post" >

more details on laravel docs

1
votes

"@mydo47: Missing method get. First you should create route with method get return view. Next, in view page you call method post validate and save to your model." this solved it