Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moses's avatar
Level 2

How to solve MethodNotAllowedHttpException in RouteCollection.php line 218:?

My view is like this :

@foreach($users as $user)
    <tr>
        <td>{!! $user->id !!}</td>
        <td>{!! $user->username !!}</td>
        <td>{!! $user->phone !!}</td>
        <td>{!! $user->address !!}</td>
        <td>
            {!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
            <div class='btn-group'>
                <a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
            </div>
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach

My routes\web.php is like this :

Route::get('users/destroy/{year}', 'UserController@destroy')->name('users.destroy.year');

Route::resource('users', 'UserController');

My controller is like this :

public function destroy($id, $year)
{
    $user = $this->userRepository->findWithoutFail($id);

    if (empty($user)) {
        Flash::error('User not found');

        return redirect(route('users.index.year', ['year' => $year]));
    }

    $this->userRepository->delete($id);

    Flash::success('User deleted successfully.');

    return redirect(route('users.index.year', ['year' => $year]));
}

There is exist error like this :

MethodNotAllowedHttpException in RouteCollection.php line 218:

And the url looks like this : http://localhost/mysystem/public/users/2?2016

When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2016

Is there any people who can help me?

0 likes
7 replies
Jaytee's avatar

MethodNotAllowed literally means the Route method is not allowed.

Look at your form, you're setting the method as 'delete' so your route in your web.php file should also be a delete request not a get

Route::delete(); not Route::get()

Take a look at this for an explanation on http requests: http://www.restapitutorial.com/lessons/httpmethods.html

1 like
ekhlas's avatar

Having Same Issue MethodNotAllowedHttpException in RouteCollection.php line 218: Form

Route: Route::post('/sub_section_edit_action/', 'Admin\CountriesController@sub_section_edit_action');

Please See : https://prnt.sc/hgashh

I can't figure our why this occurs Is there any people who can help me?

nanadjei2's avatar

the problem is not your route. It is your form. Try writing this beneath your opening form tag:

 {{ method_field('get') }}
Kenshirou's avatar

I have the same problem and don't have a clue on how to solve it.

My view is like this :

@extends('layouts.master')

@section('subtitle', 'Contact')

@section('content')
  <div class="container col-md-8 col-md-offset-2">
    <div class="card shadow-lg my-5 p-3 w-100">
      <form method="POST" action="{{ url('tickets') }}">
        @csrf
        
        @foreach ($errors->all() as $error)
          <p class="alert alert-danger">{{ $error }}</p>
        @endforeach

        @if (session('status'))
          <div class="alert alert-success">
            {{ session('status') }}
          </div>
        @endif

        <div class="card-body">
          <h1 class="card-title">Submit a new ticket</h1>

          <div class="form-group mt-5">
            <label for="ticketTitle">
              <h4 class="font-weight-bold">Title</h4>
            </label>
            <input type="text" class="form-control" id="ticketTitle" name="title" placeholder="Title">
          </div>

          <div class="form-group mt-5">
            <label for="ticketContent">
              <h4 class="font-weight-bold">Content</h4>
            </label>
            <textarea class="form-control" id="ticketContent" name="content" rows="5" aria-describedby="ticketContentHelp"></textarea>
            <small id="ticketContentHelp" class="form-text text-muted help-text">Feel free to ask us any question.</small>
          </div>

          <div class="form-group form-group-indent">
              <button type="button" class="btn btn-light btn-lg w-15 mb-2 mr-2" id="cancelButton">CANCEL</button> 
              <button type="submit" class="btn btn-info btn-lg w-15 mb-2" id="submitButton">SUBMIT</button>
            </div>
        </div>
      </form>

My routes\web.php is like this :

<?php

Route::get('/',                 'HomeController@index');
Route::get('about',         'AboutController@index');

Route::get('tickets/create',    'TicketsController@create');
Route::post('tickets',        'TicketsController@store');

My Form Request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TicketFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
                'title'     =>  'required|min:3',
                'content'   =>  'required|min:10',
      ];
    }
}

My controller is like this :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\TicketFormRequest;
use App\Models\Ticket;

class TicketsController 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()
    {
        return view('tickets.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(TicketFormRequest $request)
    {
        $slug = uniqid();
        $ticket = new Ticket([
            'title'     => $request->get('title'),
            'content'   => $request->get('content'),
            'slug'      => $slug,
            'user_id'   => 1
        ]); 

        $ticket->save();

        return redirect('tickets')->with('status', 'Your ticket has been created! Its unique id is: '.$slug);
    }
}

The model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Ticket extends Model
{
    protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
    
  public function user() 
    {
        return $this->belongsTo('User');
    }
}

Url looks like this: http://firstwebsite.test/tickets/create When I fill the fields and hit submit button it redirect to: http://firstwebsite.test/tickets but with the following error:

 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
Snapey's avatar

@Kenshirou The error is because you do not have a GET route for tickets.

So after saving, you redirect to a route that the router recognises but the verb is wrong. It knows you have a tickets route but it is POST.

Not sure where you want to go, but normally a get to tickets would hook up with the index function.

1 like
Kenshirou's avatar

@Snapey Oh right. Don't know how could I have missed it. Before I submitted the form the uri was '/tickets/create'. After submitting the form, the uri is '/tickets'. Then I redirect to the same post uri '/tickets' but I wanted to redirect back to the form at the get uri '/tickets/create'. Thanks a lot.

Please or to participate in this conversation.