Laravel 5.1 file upload error - php

I'm struggling with Laravel 5.1 LTS's requests. Every time I upload a file, I get the file name from $request->input('input_file'), but
$request->hasFile('input_file') is false and
$request->file('input_file') is null.
Frontend
{!! \Form::input('file','input_file',null,['style'=>'display:none;']) !!}
which translates to
<input type="file" name="input_file" id="input_file" style="display:none;">

Don't forget to add enctype="multipart/form-data" to your <form> element.
If you're using Laravel Collective's Form/Html package, you can pass 'files' => true to the array:
{!! Form::open(['files' => true]); !!}

I think you are missing 'files'=>'true'
Try this,
Form::open(array('url' => '/uploadfile','files'=>'true'));

Related

laravel 5.8 form::model not working, Post form is sent as Get

I have an error in one of my forms, all the others work but in particular I cannot edit in this one since it does not send the post from a form created with Form :: model (), it took me a while to realize that this was the problem, although I have defined the form as POST the data is sent as GET and it does not find the route since it is defined as POST.
This is my failed code:
{!! Form::model($nota, [
'method' => 'POST',
'url' => ['/admin/notas', $nota->id],
'class' => 'form-horizontal',
'files' => true]) !!}
#include('admin.notas.form', ['formMode' => 'edit'])
{!! Form::close() !!}
But later I realized that if I copy the html generated by this code and paste it raw, there it works!
<form method="POST" action="https://mydomain/admin/notas/{{$nota->id}}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
#csrf
#include('admin.notas.form', ['formMode' => 'edit'])
</form>
It is worth mentioning that this form allowed to edit correctly beforehand, and that in localhost (WAMP) it works correctly, I already confirmed that my model, controller and routes are fine, but everything fails from the submit and does not reach the route defined as Post because it is sent as Get.
Note:
The problem is gone, I was about to upload the form with raw html to get out of the error and I realized that the error was corrected, I do not know if it was a server error, the strange thing is that other forms did not fail and when submitting the form laravel did not throw me any error.

Is it ok to not specify POST request within form since form's default request method is POST

Good day to all, I am beginner in Laravel and as you know html form by default sends POST requests and wanted to ask if it is ok if I omit POST at the beginning of form tag since I want to send POST request to server The code:
{!! Form::open(['method' => 'ToDoController#store' **I am not specifying type of request**]) !!}
<div class="form-group">
{{Form::label('text', 'Text', ['class' => 'awesome'])}}
{{Form::text('text', '', ['class'=>'form-control' ,'style'=>'width:200px'])}}
</div>
<div class="form-group">
{{Form::label('text', 'Text', ['class' => 'awesome'])}}
{{Form::text('text', '', ['class'=>'form-control' ,'style'=>'width:200px'])}}
</div>
{{Form::submit('Submit')}}
{!! Form::close() !!}
It is OK to omit an attribute if the default is what you intend to carry out. However, you should note the following.
HTML forms by default sends 'GET' requests.
Form Collective which is a package used in Laravel Framework has by default set the method attribute of HTML forms to 'POST'.
So, the reason why your form is sending a 'POST' even when you didn't set the method attribute is because you are using Form Collective else your form would have defaulted to 'GET'.
Meanwhile, from the snippet you pasted:
{!! Form::open(['method' => 'ToDoController#store' **I am not specifying type of request**]) !!}
Change that to:
{!! Form::open(['action' => 'ToDoController#store']) !!}
The method attribute is meant for specifying the kind of request...chiefly 'GET' or 'POST' while the action attribute is meant for specifying the Controller action that will handle the request.

Delete my database record using routes in Laravel?

I'm using Laravel and I'm trying to achieve a delete function that takes care of deleting the selected task the user made.
I was wondering how can I achieve this using routes?
<button class="btn btn-outline-light">Edit</button>
<button class="btn btn-outline-light">Delete</button>
At this moment my delete route takes me to the same page as what the edit route does.
My delete function:
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks')->with('success', 'Task removed');
}
I got a working delete function using the Laravel forms:
{!! Form::open(['action' => ['TasksController#destroy', $task->id], 'method' => 'POST', 'class'=> 'float-right']) !!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-outline-danger'])}}
{!! Form::close() !!}
Is it worth to consider another method of taking care of my deletes or should I stick with using the Laravel forms?
That's because of CSRF protection. You may think using an ajax request.
You can find an example in this post.
How to delete record in laravel 5.3 using ajax request?
When you press the delete link, you'll be basically making a GET request to the tasks.destroy route, which is not what you want. You can either create a form and submit it on click (the laravel logout btn does something similar)
<a href="#"
onclick="event.preventDefault(); document.getElementById('task-destroy-form').submit();">
Delete task
</a>
<form id="task-destroy-form" action="{{ route("tasks.destroy", ["id" => $task->id]) }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
</form>
or make an ajax call that will send the id and CSRF parameters to the route.

how to set name for drop zone area in dropzone?

I am using Laravel blade, I am facing problem that am not able to get the input name for dropzone area to save to the database in dropzone.js. So I need to set name for dropzone area which must be type array.
Below is my code.
{!!Form::model(null,array('url'=>'/assets/global/plugins/dropzone/upload.php','class'=> 'dropzone dropzone-file-area', 'id'=> 'my-dropzone','files'=>'true','style'=>"width: 100%;height:50px;")) !!}
<h3 class="sbold">**Drop files here or click to upload**</h3>
{!! Form::close() !!}
Remove 'id'=> 'my-dropzone' from Form::model() and put it in div inside the form:
<div id="my-dropzone"></div>

Dropzone.js bootstrap demo not working

i have a working Dropzone.js
form upload functionality but i decided to change my form like this Dropzone.js Bootstrap demo
if i use the snippets from the demo it's not working or i don't know how to make it work
this is my old working Dropzone.js form code
{!! Form::open(['url' => route('images.store'), 'class' => 'dropzone', 'files'=>true, 'id'=>'real-dropzone', 'name' => 'addimagetoalbum']) !!}
<input type="hidden" name="album_id" value="{{$album->id}}" />
{!! Form::close() !!}
But how can i make it work using the Dropzone.js Bootstrap demo
Thank you in advance.

Categories