Reputation: 11198
I have a route resource Route::resource('projects', 'ProjectsController');
and when I run route:list
I can see POST is available.
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
| | GET|HEAD | projects | projects.index | App\Http\Controllers\ProjectsController@index | auth |
| | POST | projects | projects.store | App\Http\Controllers\ProjectsController@store | auth |
| | GET|HEAD | projects/create | projects.create | App\Http\Controllers\ProjectsController@create | auth |
| | GET|HEAD | projects/{projects} | projects.show | App\Http\Controllers\ProjectsController@show | auth |
| | PUT | projects/{projects} | projects.update | App\Http\Controllers\ProjectsController@update | auth |
| | PATCH | projects/{projects} | | App\Http\Controllers\ProjectsController@update | auth |
| | DELETE | projects/{projects} | projects.destroy | App\Http\Controllers\ProjectsController@destroy | auth |
| | GET|HEAD | projects/{projects}/edit | projects.edit | App\Http\Controllers\ProjectsController@edit | auth |
+--------+----------+--------------------------+------------------+--------------------------------------------------------------+-----------------+
When I am at /projects/create
(shows my form) and hit my submit button, I get an error saying:
MethodNotAllowedHttpException in RouteCollection.php line 201:
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 188
Is it perhaps how I am defining my <form>
tag? Am I not using the correct action?
<form method="post" action="">
I also tried <form method="post" action="{{ url('projects/store') }}">
Sorry, I am a noob to laravel!
Upvotes: 5
Views: 19049
Reputation: 655
I see that since the last reply Laravel has been updated. Anyway, I came across the same problem today and here is how I fixed it.
Basically my routing looks like this now:
// Resourcing routes: https://laravel.com/docs/5.3/controllers#resource-controllers
Route::resource('admin/photos', 'Admin\AdminPhotosController');
// need to do this to enable the store method on the following route (otherwise POST is on index when resourcing controllers)
Route::any('admin/photos/create', 'Admin\AdminPhotosController@create');
Route::post('admin/photos/create', 'Admin\AdminPhotosController@store');
Hope that helps someone.
Upvotes: 0
Reputation: 405
Laravel actually uses method="POST"
in all <form>
tags.
What you need is the following:
<input type="hidden" name="_method" value="DELETE">
DELETE
can be replaced with the other HTTP verbs too (PUT, PATCH, UPDATE, etc)
Upvotes: 3
Reputation: 892
You should be POST
ing to the resource url, not resource/create.
In other words just make sure the action of your form is action="/projects"
not action="/projects/create"
Edit: I will leave this here as it is kind of relevant, and because I originally posted it, but with the forewarning that it is overkill and a lot of irrelevant code for someone just starting.
For instance, here's a blade snippet from one of my sites:
@extends('layouts.master')
@section('title', 'Create a Project')
@section('content')
<h3>Create a Project</h3>
<hr/>
{!! Form::open(['action'=>'ProjectController@store']) !!}
@include('forms/partials/edit_form', ['submit_button_label' => 'Add Project'])
{!! Form::close() !!}
@include('errors.list')
@endsection
Upvotes: 6