This question already has an answer here:
I created a backend for my site with an "add product" function but when I add it I do not create the product on the table and I have this error
I have this error:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException
No message
My form:
@extends('admin.layout.admin')
@section('content')
<h3>Aggiungi Prodotto</h3>
<div class='row'>
<div class="col-md-8 col-md-offset-2">
{!! Form::open(['route'=>'product.create','method'=>'post', 'files'=>true]) !!}
<div class="form-group">
{{ Form::label('nome', 'Nome') }}
{{ Form::text('nome', null, array('class'=> 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('descrizione', 'Descrizione') }}
{{ Form::text('descrizione', null, array('class'=> 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('prezzo', 'Prezzo') }}
{{ Form::text('prezzo', null, array('class'=> 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('category_id', 'Categoria') }}
{{ Form::select('category_id',[1=>'Flauti'],null,['class'=> 'form-control','placeholder'=>'Seleziona Categoria']) }}
</div>
<div class="form-group">
{{ Form::label('image', 'Image') }}
{{ Form::file('image',array('class'=> 'form-control')) }}
</div>
{{ Form::submit('create', array('class'=>'btn btn-->default')) }}
{!! Form::close() !!}
</div>
</div>
@endsection
My routes:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'frontcontroller@index');
Route::get('/index.html', 'frontcontroller@index');
Route::get('/checkout.html', 'frontcontroller@checkout');
Route::get('/furniture.html', 'frontcontroller@furniture');
Route::get('/login.html', 'frontcontroller@login');
Route::get('/products.html', 'frontcontroller@products');
Route::get('/register.html', 'frontcontroller@register');
Route::get('/single.html', 'frontcontroller@single');
Auth::routes();
Route::get('/logout', 'Auth\LoginController@logout')->name('home');;
Route::get('/home', 'HomeController@index')->name('home');
Route::group(['prefix' => 'admin' ,'middleware'=>'auth'], function () {
Route::get('/', function () {
return view('admin.index');
})->name('admin.index');
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
});
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class ProductsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories=Category::pluck('nome','id');
return view('admin.product.create',compact('categories'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$formInput=$request->except('image');
//image upload
$image=$request->image;
if ($image){
$imageName=$image->getClientOriginName();
$image->move('image',$imageName);
$formInput['image']=$imageName;
}
Product::create($formInput);
return redirect()->route('admin.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* 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)
{
//
}
}
</div>
Please change following line in your form
{!! Form::open(['route'=>'product.create','method'=>'post', 'files'=>true]) !!}
to
{!! Form::open(['route'=>'product.store','method'=>'post', 'files'=>true]) !!}
basically, create routes are to show forms when you use Resources. But when you want to submit the form you need to submit to .store or if it's an edit form then submit to .update route.