major697 major697 - 1 year ago 558
PHP Question

Laravel returned unknown error in method create and store

I have a problem with my project I try write method which saved record to database. I created controller ProductsController wherein I added methods:

public function create(){
$articles = Article::lists('article_name', 'id');
$categories = Category::lists('category_name', 'id');
$date = Carbon::now();
$minDate = Carbon::minValue($date);
return view('pages.createproduct', compact('articles', 'categories', 'date', 'minDate'));

public function store(CreateProductRequest $request){
$product = new Product($request->all());
return redirect('warehouse');

Next I created file createproduct.blade.php:

{!! Form::open(['url' => 'warehouse/create', 'class' => 'form-horizontal']) !!}
{!! Form::select('article_name[]', $articles, NULL, ['class' => 'form-control']) !!}<br />
{!! Form::select('category_name[]', $categories, NULL, ['class' => 'form-control']) !!}<br />
{!! Form::text('sn', null, ['class' => 'form-control', 'placeholder' => 'Podaj serial...']) !!}<br />
{!! Form::number('quantity', null, ['class' => 'form-control', 'min' => '1', 'placeholder' => 'Podaj ilość...']) !!}<br />
{!! Form::date('warranty', $date, ['class' => 'form-control', $minDate]) !!}<br />

{!! Form::submit('Dodaj', ['class' => 'btn btn-default']); !!}
{!! Form::close() !!}

My routes.php file looks like this:

Route::get('/contact', 'PagesController@contact');
Route::resource('/addarticle', 'ArticlesController');
Route::resource('/addcategory', 'CategoriesController');
Route::resource('/warehouse', 'ProductsController');
Route::get('/home', 'HomeController@index');

And when I when I want to save the record to the database Laravel returns an error:

MethodNotAllowedHttpException in RouteCollection.php line 218

  1. in RouteCollection.php line 218

  2. at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT',
    'PATCH', 'DELETE')) in RouteCollection.php line 205

  3. at RouteCollection->getRouteForMethods(object(Request), array('GET',
    'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158

  4. at RouteCollection->match(object(Request)) in Router.php line 821

  5. at Router->findRoute(object(Request)) in Router.php line 691

  6. at Router->dispatchToRoute(object(Request)) in Router.php line 675

  7. at Router->dispatch(object(Request)) in Kernel.php line 246

  8. at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at
    call_user_func(object(Closure), object(Request)) in Pipeline.php
    line 52

  9. at Pipeline->Illuminate\Routing{closure}(object(Request)) in
    CheckForMaintenanceMode.php line 44

  10. at CheckForMaintenanceMode->handle(object(Request),
    object(Closure)) at
    'handle'), array(object(Request), object(Closure))) in Pipeline.php
    line 136

  11. at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at
    call_user_func(object(Closure), object(Request)) in Pipeline.php
    line 32

  12. at Pipeline->Illuminate\Routing{closure}(object(Request)) at
    call_user_func(object(Closure), object(Request)) in Pipeline.php
    line 102

  13. at Pipeline->then(object(Closure)) in Kernel.php line 132

  14. at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php
    line 99

  15. at Kernel->handle(object(Request)) in index.php line 53

Answer Source

Looks like there is some confusion on the resource routes...

Going off the chart here...

/warehouse/create would be the route which shows you the form and it's a GET request. You want to store the warehouse so the URL of your form should point to /warehouse and it should be POST request.

With that in mind, you should change...

{!! Form::open(['url' => 'warehouse/create', 'class' => 'form-horizontal']) !!}


{!! Form::open(['route' => '', 'method' => 'post', 'class' => 'form-horizontal']) !!}

Note that I also changed url to route because it's a little bit safer allowing Laravel to generate the URL for you rather than going with relative URLs which can easily break if your routes change for some reason or if you have this same form on another page.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download