Laravel Image Tutorials: Adding an image

Written by SarahS

1st May 2020

In this series of tutorials I am going to show you how to add images to a database and upload them to storage, then display them on your website and finally delete them from the database and storage. This first tutorial will show you how to add an image.

Firstly, I have the model, controller and database table all set up.  The name I am using is AnimalPhotoController.php, the model is AnimalPhoto and the table is animal_photos.  The table has the basic set up and the additional fields of name (this is the filename) and title.  Both are strings.

Adding an Image

So, firstly I want to add an image.

This is my route to the animalphots/create.blade.php file:

Route::get('/animals/{animal}/photos/create', 'AnimalPhotoController@create')->name('animals.photos.create');

In the AnimalPhotoController.php this is the code to take the user to the Create page:

public function create(Animal $animal) 
    {
        return view('animalPhotos.create', compact('animal'));
    }

In the create.blade.php file I have a small form for adding in the image details as so:

<form method="POST" action="{{ route('animals.photos.store', $animal) }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="name">Photo</label> 
<input type="file" class="form-control-file" name="name" />
</div>
<div class="form-group">
<label for="title">Title</label> 
<input type="text" class="form-control" name="title" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

The route to then store the image is as follows:

Route::post('/animals/{animal}/photos/', 'AnimalPhotoController@store')->name('animals.photos.store');

In my AnimalPhotoController.php I then have:

public function store(Request $request, Animal $animal)
    { 
        $attributes = request()->validate([
            'name' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'title' => 'required|string', 
       ]);
   
        $name = $request->file('name')->getClientOriginalName();
        $imageName =  date('YmdHis') . $name;
        $request->file('name')->storeAs(
            'public/images', $imageName
        );

        Arr::set($attributes, 'name', $imageName);
        $animal->animalPhotos()->create($attributes);

        return redirect('/animals');
    }

This will then save the image name and title in the database and also store the image in ‘storage/app/public/images’.

Then in your terminal you need to link the Storage folder with your Public folder so you can use the image later on. Run this to link:

php artisan storage:link

And there you are you have added an image to your application. In the next instalment I will show you how to display your image on your webpage.

This is one of my Laravel posts.  I’m still learning Laravel so the posts are usually pretty basic and are more aimed at keeping track of things that I find useful so that I can come back to them again at a later date.  I hope you find them useful too.  If not, or if you think they are wrong then please leave a comment below.  Thank you.

Faster Laravel Hosting

If this post helped you or if you have any questions please leave me a comment below:

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

Setting up SQLite for Laravel

Setting up SQLite for Laravel

Sometimes you may decide you want to use SQLite for your Laravel app instead of a full blown MySQL database. In that...