Select Page

In this post I will show you how to store images in a database in Laravel 5.1. Almost for sure in most cases better solution would be to store the images (or other files) directly in the file system. There are some advantages of storing images in database, though. For example you can replicate and backup all your data much easier. Of course the database will be bigger and backup/replication slower.

There can be also other, not technical reasons to keep the images in the database. I don’t want to explore here the question is keeping the pics in the database is politically correct or not – it’s possible so why not to give it a try?

In this example we’ll build extremely simple application which will show the web page containing a table with list of pictures, their names and id’s. It will also allow you to add a picture.

Because we’re going to use database to store the data, what we need is working and configured Laravel 5.1 installation with a database. It can be MySQL, SQLite, Postgress or SQL Server.

We also need some extra functionality:

  • HTML Forms – to make HTML forms easily – not 100% necessary but it’s nice
  • Image manipulation – to be able to do things with images


HTML Forms

Since version 5.1 to build and HTML form using HTML Forms (see: you have to install them separately as they was removed from the core of Laravel framework. Of course you don’t have to install this component and use pure HTML forms instead.

Installing HTML Forms in Laravel 5.1

Let’s begin by installing HTML Forms package through Composer. Edit your project’s composer.json file to require laravelcollective/html.

"require": {
    "laravelcollective/html": "5.1.*"
$ composer update

Next, add your new provider to the providers array of config/app.php:

  'providers' => [
    // ...
    // ...

Finally, add two class aliases to the aliases array of config/app.php:

 'aliases' => [
    // ...
      'Form' => Collective\Html\FormFacade::class,
      'Html' => Collective\Html\HtmlFacade::class,
    // ...

Image manipulation library

I’ve chosen Intervention Image package (see:
Intervention Image is an open source PHP image handling and manipulation library. It provides an easier and expressive way to create, edit, and compose images and supports currently the two most common image processing libraries GD Library and Imagick.

Installing Intervention Image

$ composer require intervention/image


Now when we have our extra packages installed we need to inform Laravel that we want access them in our application.

Add to configuration file ../config/app.php

 'providers' => [

    // ...
    // ...
'aliases' => [
    // ...
      'Image'     => Intervention\Image\Facades\Image::class,
    // ...

OK. At this point we’re done with configuration.

Creating the Application

Creating the database

To store an image in the database you have to use the blob field type. Let’s create customer database using migrations.
Open your terminal, go to the project root directory and type:

$ php artisan make:migration create_pictures_table
Created Migration: 2015_09_29_093244_create_pictures_table

Open up the migration file (should be in ../app/database/migrations/2015_09_29_093244_create_pictures_table.php) and fill it in:


     * Reverse the migrations.
     * @return void
    public function down()

Save it and back in the terminal execute the migration:

$ php artisan migrate

Now we have our database.


Route::get('list', 'PictureController@showPictureList');
Route::get('pic/{id}', 'PictureController@showPicture');
Route::get('add', 'PictureController@addPicture');
Route::post('add', 'PictureController@savePicture');

Creating the controller

$ php artisan make:controller ImageController --plain

Now we need few methods to:

  • show list of pictures
  • show a picture itself
  • add a picture to the database

Of course in real life you would like to have few more possibilities like editing and deleting.

with('pictures', $pictures);

    public function addPicture()
        return view('addpicture');

    public function savePicture(Request $request)

         $file = Input::file('pic');
         $img = Image::make($file);

         $picture = new Picture;
         $picture->name = $request->get('name');
         $picture->pic = $img;

         return Redirect::to('list');

     * Extracts picture's data from DB and makes an image 
    public function showPicture($id)
        $picture = Picture::findOrFail($id);
        $pic = Image::make($picture->pic);
        $response = Response::make($pic->encode('jpeg'));

        //setting content-type
        $response->header('Content-Type', 'image/jpeg');

        return $response;



We need two views. First to show the list of pictures and second to show the form to add a new picture. To show the mechanism I’ve created extremely simple views:

1. picturelist.blade.php

If anything needs explanation here it can be how we are showing the picture. You can’t just put {{ $picture->pic }} in the page because it will show raw binary data in the page. Instead we have to extract the data first, convert it to a proper type and return it as an image. This we’re doing using showPicture($id) method in the controller.

        Image list

My Pictures

Add picture

@foreach($pictures as $key => $picture) @endforeach
{{ $picture->id }}{{ $picture->name }}


2. addpicture.blade.php

What’s important here is that we have to explicitly say that we are uploading files with this form by adding ‘files’=>true to the array while creating the form:

Form::open(array('url' => 'add', 'files'=>true))

The view can look like this:

        Add Image

Add Image

{!! Form::open(array('url' => 'add', 'files'=>true)) !!}
{!! Form::label('name', 'Name:') !!} {!! Form::text('name') !!}

{!! Form::label('Chose the picture:') !!} {!! Form::file('pic') !!}

{!! Form::submit('Add record') !!}

Comments appreciated.