Image Credit : http://learninglaravel.net

Laravel Form Image Upload Experience

Kevin Kyaw
newborn.ninja
Published in
2 min readNov 17, 2016

--

Today, I writing a new module that can help me to upload new product image to server. First of all, I never tried with Laravel. I’m using Laravel 5.2 now. Here my some interesting code snippet.

{!! Form::open(array('url'=>'dashboard/edit','method'=>'POST', 'files'=>true)) !!}
{!! csrf_field() !!}
<label for="itemImage">Item Image</label>
<input type="file" id="itemImage" name="itemImage">{!! Form::close() !!}

After I chose the image and click on update button, I saw Class ‘Form’ not found and felt pretty strange. It worked in laravel 4. Fortunately, I found solution from stackoverflow. Because, they did some changes in laravel 5. Here my solution. Add laravelcollective/html in composer.json file

"require": {
"laravelcollective/html": "~5.0"
}

Next, run composer update and after completing composer update, adding provider and aliases under config/app.php

'providers' => [
Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],

Now, it should work file.

So, we got everything what we want. I don’t want to display validation checking detail. As a summary, here what I did to upload image.

$destinationPath = 'uploads'; // your destination path
$extension = Input::file('itemImage')->getClientOriginalExtension(); // getting image extension
$fileName = $sku.'.'.$extension; // renameing image
Input::file('itemImage')->move($destinationPath, $fileName);

Happy coding :)

--

--