I am using blade template but i was to know that is there any way to use form binding on html syntax based form?. if i would do it in blade's way it would be like
{{ Form::model( $user, array('route' => array('users.update', $user->id), 'method' => 'put' )) }}
But what if i want to use it like we add a hidden field for csrf_token() like
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
Here is my HTML form code:
<form class="form-group" action="/update" method="post" id="EditCommunityForm">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="community_name" class="form-control">
</form>
Edit:
i would like to ask that is there a way to convert this syntax {{ Form::model( $user, array('route' => array('users.update', $user->id), 'method' => 'put' )) }} to plain HTML?
You can't do model binding directly into html. You'll have to fill your form "manually". And, in your case, we will have to do a trick to overwrite the browser default methods (post/get).
Heres an example:
<form action="{{ route('users.update', $user->id) }}" method="post">
<!-- Overwrite post method as 'Put' -->
<input type="hidden" name="_method" value="PUT"/>
<!-- CSRF token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<!-- Fills an input with a model value -->
<input type="text" name="community_name" value="{{ $user->community_name }}"/>
</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 trying to pass a form through. I am using request method to get variables. here is my blade of a form:
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
Routes involved:
Route::get('/admin/gallery', 'GalleryController#manageGallery')->name('manageGallery');
Route::post('/admin/gallery', 'GalleryController#postPhoto')->name('postPhoto');
And this is my controller for it:
class GalleryController extends Controller
{
public function manageGallery() {
return view('home.manageGallery');
}
public function postPhoto(Request $request) {
die("works");
}
}
It does not throw error at me. It just does nothing. So my question is: am I using this method wrong or do I need something more? Thanks in advance.
Firstly make sure that the form you are using is using the correct method for your route
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}" method="post">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
In your controller, put the following in the postPhoto function
public function postPhoto(Request $request)
{
dd($request);
}
You should now get a Request object output to the screen when you submit the form
You may wanna use Blade Forms in order to make Forms in a more natural way for Laravel
{{ Form::open(['route' => '/admin/gallery', 'method' => 'post', 'files' => true]) }}
{{ Form::text('title') }}
{{ Form::label('title', 'Name :') }}
{{ Form::file('file') }}
{{ Form::label('file', 'File :') }}
{{ Form::submit('Add') }}
{{ Form::close() }}
It reduces the overhead of adding the token by yourself as it is automatically added when using the Form facade.
And then, in your controller, you would do something like that to debug when sending the form :
<?php
use Request; /* do not forget this line */
class GalleryController extends Controller
{
public function postPhoto(Request $request)
{
dd($request->toArray());
}
}
How to post the contents of a form to db? I know the database works.
<form method="post" action="{{ action('UserController#registration') }}">
<h1>Please Name</h1>
<textarea name="name"></textarea>
<button type="submit">Access</button>
</form>
Route::post('/registration', 'UserController#registration');
public function registration(Request $request)
{
DB::table(‘user2s’)->insert([‘name’=> $request->name]);
return 'success';
}
Add this token in your form
{!! Form::token() !!}
or this
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
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>
I have Customer Controller with index, edit, update methods
Route::resource('customer', 'CustomerController');
Controller methods update
public function update($id) { echo $id; }
my HTML Form
<form action="/customer/1" method="post">
<input type="text" name="email" value="" />
<input type="submit" value="" />
</form>
I have following a Documentation here
http://four.laravel.com/docs/controllers#resource-controllers
PUT/PATCH /resource/{id} update
It's seems not working for me, how to use it? thank you
To use the PATH, PUT or DELETE HTML methods you need to add a hidden input with _method. Like the following...
<input type="hidden" name="_method" value="PUT" />
You can use the Form Builder. Example using blade:
{{ Form::open(array('method' => 'DELETE')) }}
That will automatically add this for you
<input name="_method" type="hidden" value="DELETE">
This works for me in Laravel 4:
{{ Form::open(array('url' => URL::to('customer/1'), 'method' => 'PUT')) }}
I am using Laravel resource controller. For update a page, I copied it from insert page after then
Just I added an extra field to update view like as
{{ method_field('put') }}
Just use this as for update
<form method="post" action="{{ URL::to('customer',$customer['id'])}}">
{{ csrf_field() }}
{{ method_field('put') }}