Photo Management with Laravel — Save in Database


In this tutorial, you will save the photos in database.


As I mentioned, you always can jump into any topic in this series. The setup instructions are at README.

Once your setup is done, let’s get it started.

First thing first, you need a database.

Setup database

Create a database if you don’t have it yet, and configure it in your application.

// .env

Next, create a table.

Create photos table

You create a Photo model and a migration file by executing artisan command make:model with -m option.

// Terminal
php artisan make:model Photo -m
Model created successfully.
Created Migration: 2017_09_28_144426_create_photos_table

Then, add a field in the photos table.

// database/migrations/2017_09_28_144426_create_photos_table.php
public function up()
Schema::create('photos', function (Blueprint $table) {

Run migration.

// Terminal
php artisan migrate

Next, save in database.

Save in database

You save a new Photo model after the uploaded photo stored in the storage folder.

// app/Http/Controllers/PhotoController.php
public function store(UploadPhotoRequest $request)
foreach ($request->file('photo.*') as $key => $file) {
$extension = $file->extension();
$path = $file->storeAs('images', "my_photo.$key.$extension");

$photo = new Photo();
$photo->path = $path;

return view('success');

YouTube version

I always think how can I help you to learn Laravel faster and easier. If you’re a fan of learning by watching, check out the video version of this tutorial in YouTube.

What’s next

Next tutorial will be talking about displaying the photos.

If you haven’t subscribed to my email list on the homepage, do it now. I’ll notify you when the new tutorial is published.

Did I tell you that you’ll receive a series of FREE tutorials when you subscribe? Go check it out.

Stay tuned.

Originally published at I Teach You How To Code.