How to implement Laravel CRUD with preview and upload image
A CRUD application provides the basic database operations, such as, SELECT, INSERT, UPDATE AND DELETE. In this topic, we will develop a Laravel CRUD application, in which you can view the list of users, add new user, update an existing user and also delete an user. CRUD full form is Create(C), R(Read), U(Update) and D(Delete). User will have a photo also. So while adding you can preview and upload the photo and also change the photo. So we will show Laravel image CRUD also in this application.
Let us create a project in Laravel. Make sure you have composer and PHP installed in your system. I have given the project name as lara_crud_image.
This will create a folder named "lara_crud_image" under the folder where you run the above command.
Step 2 - MySQL Database and Migration
Update .env file for database details. I am using MySQL database named 'lara_demo', so I updated the file as below:
We will use Laravel "users" table for this topic. We will add a nullable column "photo" to this table. Let us create a migration to alter the table named 'users'. Go to your project folder and run below command for migration from VS code terminal.
Above command will create the migration file name <yyyy_mm_dd_xxxxxx>_add_photo_to_users_table.php under database/migrations folder.
Update migration file
Let us update the above migration file as below:
So, "users" table will have an additional column "photo" after the "email" column.
Let us now run Laravel migration to create the default Laravel tables as well as altering the users table. Run below command from project folder to create the tables.
After running migration see the structure of 'users' table.
You can see photo column added in the "users" table.
Also add photo field as $fillable in User model.
Step 3 - Write Controller code
Let us first take a look at the home page which lists all users and for each user there is an edit and a delete buttons. Also user can be created by clicking on the "New User" button.
It displays the existing users' details along with photo. You can add new user. You can edit the details, update the photo, delete the user.
We will have a resource controller which will have methods for each of the above functions. Let us create the controller as below:
Controller will have below methods:
index() method to display all the users.
create() and store() for creating new user and saving into the database.
edit() and update() methods to update the user details along with photo.
destroy() method to delete as user.
You can see it covers all CRUD operations (C-Create, R-Read, U-Update, D-Delete). Let us see the code for the controller
index() method
We just get the users and load the view index with the data. This will display the user details along with the photo.
create() and store() methods
In store() method we are validating the data submitted from the add user form. Since Photo is not mandatory, we need to check if photo exists. If exists, we upload it in "public/uploads" folder and use the photo file name to insert into the database.
edit() and update() methods
I am using the same form for both Add and Edit user.
edit() method is called when user clicks on Edit button. It uses the id of the user and gets the data from the database to display details in the form.
In update() method we are doing the same validation for add user and checking if photo is selected. We upload the photo and delete old photo for the folder. Then update the database with the new values along with the photo.
destroy() method
destroy() method is to delete the selected user. See, it deletes the user from the user table. Also, if photo exists for the user, it deletes the photo of the same user form the folder. There will be a delete confirmation from the user.
Routes
Step 4 - Create the blade views
We will create two views, one for listing the users and the other adding/updating the user. Along with them we will have one modal for user confirmation before deleting.
I have below files in the resources/views folder
header, footer and master are the layouts. add_user.blade.php is for the adding/updating user. index.blade.php is for listing of the users. modal_delete.blade.php for user confirmation before delete.
resources/views/layouts/header.blade.php
resources/views/layouts/footer.blade.php
resources/views/layouts/master.blade.php
resources/views/index.blade.php
resources/views/add_user.blade.php
resources/views/modal_delete.blade.php
I am using some styles and the stylesheet is given below:
public/css/style.css
Step 5 - Test the application
From the project root, start the php development server by running php artisan serve command:
From browser start localhost:8000. Verify if application is working correctly. Test below cases: