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') }}
Related
I want delete a comment, but when i click, this redirect me to laravel error page and display "" that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
#foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
#endforeach
If you add {{ method_field('DELETE') }} on the html form, you must change the route method to delete:
Route::delete('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
But if you want to use POST method on the route, you must remove {{ method_field('DELETE') }} from your form.
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());
}
}
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>
I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Below are route listing:
HTML forms don't actually support any methods other than GET and POST. To get around this Laravel spoofs the method and then picks this up in the request.
From the 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
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method with {{ method_field('DELETE') }} using Blade.
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
I'm trying to use the php upload_progress feature with symfony2. I set my session.upload_progress.prefix and session.upload_progress.name in my php.ini.
My form below :
<form id="form-import-file" action="" method="post" {{ form_enctype(form) }} class="form-horizontal">
{{ form_widget(form.file, { 'attr':{'class':'input-file-import'}}) }}
{{ form_rest(form) }}
{{ form_errors(form.file) }}
<input type="hidden" name="{{ upload_progress_name }}" value="123" />
<button type="submit" class="btn btn-success">Submit</button>
</form>
where upload_progress_name = ini_get("session.upload_progress.name").
The upload is okay, but the session doesn't show any upload infos.
Any help ?
Ensure that <input type="hidden" name="{{ upload_progress_name }}" value="123" />
is before the file input fields. Just place it under the form tag.
Also what helped me out is this list: issues on php upload progress
Finally there is good symfony2-bundle, which may help you to not reinvent everything new.OneUpBundle
With this bundle you can pick a Frontend solution or create your own one.