Laravel 5.7 CRUD Example Tutorial From Scratch For Beginners

Welcome to Laravel 5.7 CRUD Example Tutorial From Scratch For Beginners.

We will create a simple application in laravel which will have create, read, update and delete tasks for MySQL database.

Version of laravel would be 5.7 and we will make a list of super heroes in this example.

See the CRUD Operations

First of all, check out the last output of this example in below video.

Step 1. Make a new project and database

First of all, create a new project using the below command

Above line will make a new project named “laraCrud

I am using sequal pro to handle MySQL database here. You can also use phpMyAdmin also for this.

Now make a new database using sequal pro or phpMyAdmin and name of database should be “superhero

So we have now a “superhero” database. Time to create a new table named “heros

Now in your .env file , you need to add this database name. So edit the below lines

Hit the below command

Now we should have one new migration file inside database->migrations directory.

This file should have name like 2018_09_create_heros_table.php 

Do not change name of this migration file. Now a source code for this file is as the below

Inside up() function, I have added two lines like the following

These lines will create two fields named “name” and “work” in the “heros” table.

Now fire the below command to finally create a heros” table.

Step 2. Making a model and Controller

Now we will create model and controller class. First of all, hit the below command

Now see that we are telling compiler to make a controller class named “HerosController” and a model named “Hero

We do not have model class right now, so it will ask us if we want to create model or not. say yes for this.

See the below lines to create controller and model successfully.

Now navigate to app->Hero.php file. It should have the below code

You need to add a line ”   public $table = “heros”;    ” as per the above lines.

Now go to the app->http->controllers->HerosController.php file

You will see that this file already contains methods like index(), create(), show(), store(), update() etc.

You need to write some lines in these methods. So these lines are as the below

Above is the complete controller class which is able to make operations like create, update, fetch and delete from database.

I will explain this methods later.

Step 3. Making routes

A file called web.php (inside routes folder) has the below code structure.

Look at the last line. You might wonder that only one route line??? Yes, because it has defined many routes indirectly.

To see all these routes, hit the below command in your terminal.

You should see the below output

So you can see that we have many routes there.

Now we will create blade files for every route step by step.

Step 4. Making Blade View files

We will start with index file.

Fetching all the heros

Our first task is to get all the names of the super heros from the database.

For this, we need to visit “” in the browser. When you visit this URL, laravel will run the index() function from the controller class.

index() function from the controller class is as the below

First line will fetch all the records from the table.

Second line will return the index.blade.php file. So we need to create this index.blade.php file file.

But first make a new folder named “heros” inside resources->views directory.

Now inside this heros folder, make a new file named index.blade.php and copy the below source code in it

See the very first line. It is extending ‘layout’.  It is referring to the layout.blade.php file which we have not created yet.

So go on and make new file (layout.blade.php) in resources->views directory. Code snippet for this file is as the below

Creating a new Hero

Index file contains one link, when the user clicks this link, laravel will open a new screen. You will be able to add new hero from this screen.

Behind the seen, when the user hit the link, laravel will run the create() method from controller class.

Code for this create() method is as the below

This function is telling the compiler to load create.blade.php file.

So make a new file named create.blade.php inside resources->views->heros directory and write down the below code in it

Above file create one form which has two input fields. User can enter hero’s name and work in it.

When the user hit the “Make Super Hero” button, laravel will add one new hero into the database.

For this, compiler will execute the store() function from controller class.

Above snippet will add the new hero into the table.

Edit and Delete Database Records

Now in the index view, you will see all the names of the heroes.

When the user clicks on any hero, compiler will open a new screen. For this it will run the show() function from controller file.

Below is it’s source lines

This function will open the show.blade.php file. Make a new file (show.blade.php) under resources->views->heros directory and below lines in it

This code will print the name and work of the heroes first.

Below them, there is a link like “Edit Hero

Click on this link will open up a new page. It will run the edit() function from controller.

Above function will open edit.blade.php file. Make a new file (edit.blade.php) under resources->views->heros directory and below lines in it

This page will have two fields. User can change or edit hero information from here.

There are two buttons : Update Hero and Delete Hero

When the user clicks the Update Hero button, compiler will run the update() function which have below source code

It will simply update the name and work of the hero.

When the user clicks the Delete Hero button, compiler will run destroy() function which have below code lines

Above will delete the hero from the database.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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