I'm creating a laravel application that allows users to create a blog post.
I have created a PostsController as a resource with store function like this:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
Also, I added a route in web.php
Route::resource('posts', 'PostsController');
If I list the routes with php artisan php artisan show:routes, POST method is listed:
The HTML form looks like this:
<form action="PostsController#store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
When I submit the form, I get MethodNotAllowedHttpException:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I used to use laravel collective for forms before. Haven't done any work in laravel for a while and now it seems to be deprecated (https://laravelcollective.com/), so I resorted to classic HTML form. How do I work around this?
Your action is incorrect within your form - you need to point the action to the URL of the route, and then the route will select the method, in this case, the 'store' method. Also add #csrf for more information CSRF Protection
<form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
add the name in textbox and textarea
form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
Related
I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so it makes me more confused of what's wrong because i really don't know why.
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/update','ContactController#update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your update route is using the wrong "verb" and url. If you take a look at Laravel's Resource Controllers you can see the different actions and route names available for editing, updating, deleting etc. when creating a "CRUD" controller.
You can see the route for an "update" action and its "verb".
Change your routes to
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Or if you want to add a complete CRUD controller use the short form:
Route::resource('contact', 'ContactController');
This will create all required routes in one convenient line of code. Use php artisan route:list to check all routes.
HTTP forms only support GET and POST methods, Laravel uses #method() in blade to add the other verbs (put,patch,delete):
Edit:
your form uses disabled attributes on some <input>s. Those values will not be sent along with your request. Here's an updated edit.blade.php:
disabled attributes have been swapped with readonly.
The action uses Laravel's RouteModelBinding
Removed the #foreach since you only have one item to be edited
edit.blade.php:
<div id="contact">
<h2>Edit Contact</h2>
<form action="{{ route('contact.update', ['contact' => $contact]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $contact->kode_kontak}}" readonly>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $contact->kode_pegawai}}" readonly>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $contact->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $contact->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
</div>
Since it's using RouteModelBinding you can change your update() method to:
public function update(Request $request, Contact $contact)
{
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Laravel will know what Contact $contact is
you loop your contact to produce a form for each one of them with the same input name, id,... which is the wrong approach.
my suggestion to you :
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/{contact}/edit', 'ContactController#update');
Controller with edit and update function
public function edit(Contact $contact){
return view('contact.edit',compact('contact'));
}
public function update(Request $request,Contact $contact){
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
and of course, you will update edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/{{ $p->id }}/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
i'm working on a crud app to learn laravel i'm doing good so far , other than when i want to update a post it gives me this
put method is not supported for this route
#extends ('layouts.app')
#section('content')
<form action="{{route('update_blog_path',['blog'=>$blog->id])}}" method="POST">
#method('PUT')
#csrf
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
Try please;
#extends ('layouts.app')
#section('content')
<form action="{{ route('update_blog_path', ['blog' => $blog->id]) }}" method="POST">
#csrf
{{ mehod_field("PUT") }}
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
and
Route
<?php
Route::put('update_blog_path/{blog}', 'BlogController#update')->name("update_blog_path");
And your'e code wrong update term
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
change update
Looks like you have a typo in your routes file, change
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
to
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#update');
You misspelled the method name update.
I'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error
Here's the controller
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
Here's my view create.blade.php
<form action="post.store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
i am assuming your are using resource route.
i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.
<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}
Example:
In route list is shown posts.store, so you will call in view {{ route('posts.store') }}
I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store
use in form action {{ route('posts.store') }}
route ::post()is not working fine on the other hand route::get() is working fine and other method is working, just post method is not working
Route::get('/', 'HomeController#index');
this route is working:
Route::post('/posts', 'Cdesignation#index');
but this route is not working
use same form:
<form action="/form/process" method="POST">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div> <input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="submit" class="btn btn-default" value="Submit">
</form>
how to resolve it
if you notice that the route in the form is /form/process it should be /posts
am try to send request by post form from but error TokenMismatchException
here is my controller code
public function postSaveedit(Request $request) {
$var1 = $request->input('title'); //name on the form
$var2 = $request->input('meaning'); //name on the form
$words = User::where("title", $var1)->update(array("meaning" => $var2));
return view('dict.watch', compact('words'));
}
here is view code.
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>
thnak you for your help
From https://laravel.com/docs/master/routing:
Laravel automatically generates a CSRF "token" for each active user
session managed by the application. This token is used to verify that
the authenticated user is the one actually making the requests to the
application.
Anytime you define a HTML form in your application, you should include
a hidden CSRF token field in the form so that the CSRF protection
middleware will be able to validate the request. To generate a hidden
input field _token containing the CSRF token, you may use the
csrf_field helper function
Just add this line inside your form
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>