Reputation: 49
post method in Laravel give MethodNotAllowedHttpException
Html code
<form action="newslatter" method="post">
<input type="text" name="name">
<br>
<br>
<input type="email"name="email">
<br>
<br>
<input type="submit">
and controller code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class newsletter extends Controller
{
public function store(Request $request)
{
return $request -> name .' '. $request -> email;
}
and Route
Route::POST('Newslatter','newsletter@store');
What problems are my codes?
err pic
Upvotes: 2
Views: 768
Reputation: 454
Why not just use laravel recipes they are cool
{{ Form::model($role, array('route' => array('roles.update', $role->id), 'method' => 'PUT', 'class'=>"form-horizontal form-label-left","novalidate"=>'true')) }}
declares a form passing the $roles variable from the backend and generates the csrf_field for you(solving the above problem).
also within the form
{ Form::text('name', null, array('class' => 'form-control col-md-7 col-xs-12','required'=>'required','id'=>"name")) }}
to display the name property in the passed $user object(avoiding lenghty empty condition checks on your html)
Upvotes: 0
Reputation: 1043
I think this problem occurs because of
<form action="newslatter" method="post">
Please try as
<form action="{{ route('route-name') }}"> metho="post">
{{ csrf_field() }}
And
Route::post('/newslatter', 'ControllerName@methodName')->name('route-name');
Upvotes: 3
Reputation: 163748
Change Newslatter
to newslatter
in the route to make it work:
Route::post('newslatter', 'newsletter@store');
Also, add CSRF field to the form:
<form action="newslatter" method="post">
{{ csrf_field() }}
In Laravel 5.6+:
<form action="newslatter" method="post">
@csrf
Upvotes: 6