Laravel 5: cannot POST route a resource

I have a route resource Route::resource('projects', 'ProjectsController'); , and when I run route:list , I see that 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 in /projects/create (shows my form) and press the submit button, I get an error message:

 MethodNotAllowedHttpException in RouteCollection.php line 201: at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 188 

Is it possible that I define my <form> ? Am I not using the correct action?

<form method="post" action="">

I also tried <form method="post" action="{{ url('projects/store') }}">

Sorry, I'm noob to laravel!

+6
source share
3 answers

You should be POST to the URL of the resource, not the 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 because it is very relevant, and because I originally posted it, but with a warning that this is redundant and a lot of irrelevant code for someone just starting out.

For example, here is a fragment of a fragment 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 
+7
source

Laravel actually uses method="POST" in all <form> tags.

You need the following:

 <input type="hidden" name="_method" value="DELETE"> 

DELETE can also be replaced by other HTTP verbs (PUT, PATCH, UPDATE, etc.)

+3
source

I see that since the last answer Laravel has been updated. Anyway, today I ran into the same problem, and here's how I fixed it.

Basically my routing looks like this:

 // 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 this helps someone.

0
source

All Articles