Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: Can't POST to route resource

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!

like image 497
Ronnie Avatar asked Jul 23 '15 22:07

Ronnie


2 Answers

You should be POSTing 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
like image 178
Zerp Avatar answered Nov 15 '22 00:11

Zerp


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)

like image 32
Kyle Avatar answered Nov 14 '22 23:11

Kyle