I want to submit a form in laravel 5 but it does not call the update function.
<form class="form-horizontal", role="form" method="patch" action{{url('/user/'.$user->id) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ $user- >name }}">
</div>
</div>
</form>
The action attribute is not complete, should be action="..."
You can also use route instead of url e.g.
In blade:
<form class="form-horizontal", role="form" method="patch" action="{{ route('user.show') }}">
In routes.php:
Route::get('user/{id}', [
'uses' => 'UsersController#action',
'as' => 'user.show'
]);
The action is incorrect. Missing = and beginning ". You also have a , after class. Not valid html.
If you want to pass parameters to an URL then maybe you should consider this.
url('foo/bar', $parameters = [], $secure = null);
Your html is invalid, you are missing the action attribute. HTML forms should generally take the form:
<form action="where/form/submits/to" method="form-method"> ... </form>
see this for details on the form element.
In your case that will is should be like:
<form class="form-horizontal", role="form" method="patch" action="{{url('/user/'.$user->id) }}"> ... </form>
Related
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
I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Here are my routes:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController#edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController#update');
And Here are the methods being called:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
Why am I getting a 404 error and how do I fix it?
Thanks,
In laravel docs, 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:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
There are so many issues in your code lets resolve one by one:
action="/admin/professions-update/{{ $data->pkprofession }}">
change it to:
action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">
and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:
<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
#method('PUT')
#csrf // this is required when you are using the method other then 'get'
other elements
</form>
You're missing the csrf token and the method input. Try this:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Also, in your update method you are forgeting to update the object, add this to your code:
$data->update($request->all());
For more info: DOCS
Is that form action there need to pass parameter for ID?
Here is the route file web.php
Route::get('/customer/account/edit/{id}',['as'=>'admin.edit','uses'=>'AdminController#edit']);
Here is the view file create.blade.php
<form action="{{ route('admin.edit') }}" method="POST">
{{csrf_field()}}
<div class="fieldset">
........
</div>
</form>
First of all you need to set routr as post and You need to specify the route definition like below if id is an optional parameter
Route::post('/customer/account/edit/{id?}',['as'=>'admin.edit','uses'=>'AdminController#edit']);
Or else it should be like below
<form action="{{ route('admin.edit', ['id' => $id]) }}" method="POST">
{{csrf_field()}}
<div class="fieldset">
........
</div>
</form>
I'm trying to create a form that passes data via get to the controller but the URL looks allways like this:
http://example.com/test?_token=VinwWFxKIhKvMqrrEBN5xwXhrmYQjLnOWV8s7dht¶m1=horse¶m2=cat¶m3=dog
But I want something like this:
http://example.com/test/param1=horse/param2=cat/param3=dog
or
http://example.com/test/horse/cat/dog
Route:
Route::get('test/{param1}/{param2}/{param3}', ['as' => 'test', 'uses' => 'MainController#test']);
HTML:
<form action="{{ route('test') }}" method="get">
{{ csrf_field() }}
<div class="col-md-3">
<div class="form-group">
<label for="animal1">animal1</label>
<br>
<input type="text" name="animal1" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal2">animal2</label>
<input type="text" name="animal2" class="form-control">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="animal3>animal3</label>
<input type="text" name="animal3" class="form-control">
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
The problem is that the test route is reloaded every 10 seconds. Therefore, the form values must be in the URL so that I can process them correctly in the controller.
I've found this question here but that wasn't so helpful
How To Pass GET Parameters To Laravel From With GET Method ?
Thanks for your help!
To do this, you'll need to switch off CSRF token check, which is a bad idea. Or you could use JS to build the query which is not a good idea too.
The best way to handle this is to use POST instead of GET:
<form action="{{ route('test') }}" method="post">
And then change the route to:
Route::post('test', ['as' => 'test', 'uses' => 'MainController#test']);
You can do this via javascript.
You don't need to use form like this.you can just get input values by id(getElementById) and on a button click, format them as you expect (test/{param1}/{param2}/{param3}) and redirect page to that.
My routes is here
Route::get('sign-up', ['as' => 'signUp', 'uses' => 'UserController#signUpGet']);
Route::post('sign-up', ['as' => 'signUpPost', 'uses' => 'UserController#signUpPost']);
Controller
return redirect('signUp')->withInput();
And View
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
The {{old()}} function return empty value.
EDIT
I took
NotFoundHttpException in RouteCollection.php line 145:
Your problem looks like you are not actually submitting the username in the first place:
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>
There is no 'submit' button inside the form. If you submit outside the form - then the username will not be included.
Add the submit button inside your form - then try again
<form role="form" method="POST" action="{{route('signUpPost')}}">
<input type="text" class="form-control" name="username" value="{{ old('username') }}">
<input type="submit" value="Submit">
</form>
Edit - also your controller is wrong. It should be this:
return redirect()->route('signUp')->withInput();
All you are missing is to Flash the Input to the session. This is so it's available during the next request.
$request->flash();
Do that just before calling to View your form.
Source: http://laravel.com/docs/5.1/requests#old-input
<div class="form-group #if($errors->first('username')) has-error #endif">
<label for="username" class="rtl">Enter user name </label>
<input type="text" name="username" class="form-control rtl basic-usage" id="username" placeholder="Enter user name" value="{!! old('username') !!}">
<span class="help-block required">{{$errors->first('username')}}</span>
</div>
above form field will show old value entered.
will show validation error (you have to specify error separately.)
have a place holder.
bootstrap to look better.(add bootstrap)
Your name in the register view should correspond with keys in create() and validate() method inside your RegisterController file.
My problem here was caused by "data-prefill" in the input. Once I removed this, it worked.
You can try this: {{ Input::old('username') }}.