Reputation: 1862
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){
dd($request->article_name);
$product = new Product($request->all());
Auth::user()->products()->save($product);
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::auth();
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
- in RouteCollection.php line 218
- at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 205
- at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158
- at RouteCollection->match(object(Request)) in Router.php line 821
- at Router->findRoute(object(Request)) in Router.php line 691
- at Router->dispatchToRoute(object(Request)) in Router.php line 675
- at Router->dispatch(object(Request)) in Kernel.php line 246
- at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
- at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
- at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
- at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
- at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
- at Pipeline->then(object(Closure)) in Kernel.php line 132
- at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
- at Kernel->handle(object(Request)) in index.php line 53
Upvotes: 1
Views: 1177
Reputation: 33118
Looks like there is some confusion on the resource routes...
Going off the chart here... https://laravel.com/docs/5.2/controllers#restful-resource-controllers
/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']) !!}
to
{!! Form::open(['route' => 'warehouse.store', '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.
Upvotes: 2