Hola
Hola

Reputation: 2233

Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219

I want to save one form data through my task controller. But when i go to url to access my form. it's showing the following Error:

MethodNotAllowedHttpException in RouteCollection.php line 219:

Here is my Routes.php

<?php
    Route::group(['middleware' => 'web'], function () {
    Route::auth();

  Route::get('/', function () {
    return view('welcome');
    });

    Route::get('/all_item','TestController@index');
    Route::post('/create_item','TestController@create');
    Route::get('/home', 'HomeController@index');
});

Here is my TaskController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Test;
use App\Http\Requests;
use Redirect;

class TestController extends Controller
{
   public function index()
    {
            $alldata=Test::all();
    //      return $alldata;
            return  view('test.itemlist',compact('alldata'));
    }


    public function create()
    {
            return view('test.create_item');
    }


    public function store(Request $request)
    {       
            $input = $request->all();
            Test::create($input);       
            return redirect('test');

    }   
}

Here is the create_item page( post form / view page)

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Create Item</div>
                {!! Form::open(array('route' => 'Test.store','class'=>'form-horizontal','method' => 'patch'))  !!}
                {!! Form::token(); !!}
                  <?php echo csrf_field(); ?>
        <div class="form-group">
          <label>Item Code</label>
          <input type="text" name="item_code" class="form-control"  placeholder="Code">
        </div>
        <div class="form-group">
          <label>Item Name</label>
          <input type="text" name="item_name" class="form-control"  placeholder="Name">
        </div>        
        <button type="submit" class="btn btn-default">Submit</button>
               {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection

Upvotes: 2

Views: 2441

Answers (3)

The Odd Developer
The Odd Developer

Reputation: 929

As i can see TestController@create is a post method.But it behaves like a get method.Try passing Request $request parameter to the create method.Or else if you really need a get method for the create method, change the method as get in Routes.php as like this,

Route::get('/create_item','TestController@create');

Upvotes: 0

Jim M
Jim M

Reputation: 4341

LaravelCollective's HTML only supports methods POST, GET, PUT DELETE so you might want to change that to POST or PUT

'method' => 'POST'

You haven't declared a Test.store route in your Routes.php, so try adding a resource or a named route:

Route::post('/store_item', [
    'as' => 'Test.store', 'uses' => 'TestController@store'
]);

Upvotes: 1

Arthur
Arthur

Reputation: 2889

You're using PATCH method in form, but route with POST method

try

'method' => 'patch'

change to

'method' => 'post'

Upvotes: 1

Related Questions