How to update a Table from Form data in Laravel 5 - php

I have a form in Laravel5
<form method="POST" action="http://localhost:8000/song/Baby/update" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="kagIHsGe3zOZSPVyW6wW84Cn5eresZ2nlF287nNK">
<div class="form-group">
<input class="form-control" name="title" type="text" value="Baby">
</div>
<div class="form-group">
<textarea class="form-control" name="lyrics" cols="50" rows="10">
Yo Yo Yo BABY
</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update Song">
</div>
</form>
Then in Route file I have written the code
patch('songs/Baby/update','SongsController#update');
Its throwing error
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 143:
Is the syntax changed for PATCH request in Laravel 5?

Your route and form action are different.
You have defined a route with songs (plural) and used as song (singular) in form action.
Try changing your form action to
action="http://localhost:8000/songs/Baby/update"

Try this: <input type="hidden" name="_method" value="PUT"> and Route::put('songs/Baby/update','SongsController#update').

Related

Why do I keep getting "The GET method..." when I not using the get method? [duplicate]

This question already has answers here:
Error 405 (Method Not Allowed) Laravel 5
(9 answers)
Closed 1 year ago.
Why do I keep getting this error?
Route::post('/dashboard/project/create/project/1', [DashboardController::class, 'create'])->name('project.create');
form:
<div class="border-color col-md-10">
<div class="profile">
<form action="/sender" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
</form>
</div>
</div>
link to create
Create Project
Your form (action="/sender") is not submitting to the route you posted.
<a href="{{route('project.create')}}"> opens the route, but using GET.
Use the route name in your form's action to send your form as POST.
<div class="border-color col-md-10">
<div class="profile">
<form action="{{route('project.create')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
</form>
</div>
</div>

Laravel form submission doesn't goes to the right route?

I have installed the latest laravel. I Have made this simple form. I want to create post but when I submit it goes to localhost/post which is the wrong URL . The actual URL is http://localhost/laravel_practice/'
Form
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
My Routes
Route::get('/' ,'PostController#index');
Route::get('/posts/create', 'PostController#create');
Route::post('/post','PostController#store');
Your short fix is to use action="/laravel_practice/post" or action="/laravel_practice/public/post" depending on what url you want to go.
However, it is a bad practice. You should use route name. To do that give any name to the route like below,
Route::post('/post','PostController#store')->name('post.store');
Then in view you should use,
<form method="post" action="{{ route('post.store') }}">
To read more about named route you can go through this document.

Why is Post method giving the error MethodNotAllowedHttpException in Laravel

I am trying to submit a form in Laravel but I am getting the error The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I have tried the suggestions in post method in laravel give MethodNotAllowedHttpException but none is working.
Here is my code.
<div class="row" style="background: #ffffff;">
<div class="col-lg-12 col-md-12 col-sm-12" style="background: white; margin: 10px">
<form method="post" action="{{ route('companies.update',[$company->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="method" value="put">
<div class="form-group">
<label for="company.name">Name <span class="required">*</span> </label>
<input placeholder="Enter name" id="company-name" required name="description" spellcheck="false" class="form-control" value="{{ $company->name }}" />
</div>
<div class="form-group">
<label for="company-content">Description</label>
<textarea placeholder="Enter Description" style="resize: vertical" id="company-content" name="description" rows="5" spellcheck="true" class="form-control autosize-target text-left">
{{$company->description}}</textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</form>
</div>
</div>
Replacing post with get,put removes the error but not doing what I want.
These are my routes
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('companies','CompaniesController');
Route::resource('projects','ProjectsController');
Route::resource('roles','RolesController');
Route::resource('tasks','TasksController');
Route::resource('users','UsersController');
In the CompaniesController I have
public function update(Request $request, Company $company)
{
$companyupdates = Company::where('id', $company->id)->update([
'name' => $request->input('name'),
'description' => $request->input('description'),
]);
if($companyupdates){
return redirect()->route('companies.show', ['company'=>$company->id])->with('success','Company Updated Successfully');
}
return back()->withInput();
}
Where am I going wrong?
Try using the blade directives instead:
<form method="post" action="{{ route('companies.update',$company->id) }}">
#csrf
#method('PUT')
Note: you don't need to pass the company id with '[ ]'
In this input:
<input type="hidden" name="method" value="put">
The name should be _method according to the laravel form method spoofing
Example from the docs:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
With the blade directives:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>`
Why is this error occurring?
You put the wrong name on your method input, so laravel will recognize this form action as POST, and not PUT. Since it's a update action, laravel will thrown this error.
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
For more info: Docs

laravel 5.3 MethodNotAllowedHttpException in RouteCollection.php line 218:

(SOLVED) Thanks..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. What's wrong with my route?
Here is my form:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route:
Route::post('/tambah', 'JurnalController#tambahJurnal');
And it show an error like this:
enter image description here
You are creating a route for the POST method with this line:
Route::post('/tambah', 'JurnalController#tambahJurnal');
But then, you're trying to perform a GET request with your browser on that URL. That's why you're getting that error.
Try adding this line as well:
Route::get('/tambah', 'JurnalController#tambahJurnal');
#1. Add this route in your route file.
Route::get('/tambah', function()
{
return view('tambah');
});
#2. change in tambah.blade.php file
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks
You can write this. Hopefully this will solve your problem.
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah"
try with
Route::any('/tambah', 'JurnalController#tambahJurnal');
first then if it works fine you can change to
Route::post('/tambah', 'JurnalController#tambahJurnal');
any will work for get post put ....
Change this line
Route::post('/tambah', 'JurnalController#tambahJurnal');
to
Route::post('tambah', 'JurnalController#tambahJurnal')->name('tambah');
and use blade Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form is , you don't explicitly need to specify {{ csrf_field() }}, blade injects csrf token itself.
Add route to show view
Route::get('/tambah', 'JurnalController#index');
And add index method to your controller
public function index(){
return view("tambah");
}
Also add /
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah and trying to post url becomes /tambah/tambah

my GET route not working in laravel

I am new to laravel and working on a form.
This is the form
<form action="/product" method="GET">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter product name" />
<div class="input-group-btn">
<input type="submit" class="btn btn-danger" value="Search" />
</div>
</div>
And this is the Route I have written
Route::get('/product/{product}', 'FlashCartController#find_product');
When I submit my form it says
NotFoundHttpException in RouteCollection.php line 161:
How do I submit this form?
In this case you need product ID in the form action. For example
<form action="/product/{{$productId}}" method="GET">
If you just want to create new product then lose the {product} and change the GET to POST as forms are submitted with mostly with POST.
<form action="/product" method="POST">
Route::post('/product', 'FlashCartController#find_product');

Categories