Laravel 5.8 CRUD Tutorial With Example Step By Step

Laravel 5.8 CRUD Tutorial With Example Step By Step is explained today.

We will implement create, read, update and delete functionalities of data records using MySQL database.

Laravel is powerful PHP framework which provides healthy built-in features which simplifies the process of interacting with the database.

We will create and update sports players and will also learn how to fetch and delete records from MySQL database using Laravel 5.8.

Watch the following video which is outcome of this Laravel 5.8 CRUD Tutorial.

Step 1. Create Laravel 5.8 Project

Use the below command to create laravel 5.8 project.

  • Above command will make laravel 5.8 project and name of the project will be lara5.8Crud
  • Don’t worry if your current version of laravel is lower than 5.8, this command will still create project which uses laravel 5.8 version.

Step 2. Making MySQL Database

Now we need to make a new MySQL database named “crud58players”

  • I am using sequel pro to manage MySQL database. If you are using phpMyAdmin with XAMPP or WAMP or MAMP then it is all ok. Process is same.
  • Just create a new database and name it as “crud58players”
  • Now in your laravel project, open up your .env file.

In this file, you need to change below lines

  • My username is “root” and password is empty. Similarly, update all the information as per your system configurations.

These lines will connect our laravel project with the database.

Step 3. Creating New Table in Database

Time to create new table in our database.

Trigger the below command in terminal

  • Using above command, system will create a new migration file in laravel project.
  • Browse in app->database->migrations folder. Inside migrations folder, you will find a new file which may have name like timestamp_create_player_table.php file.

Source code for this timestamp_create_player_table.php file is as the below

Look at the up() function. This function includes the name of the various fields (column names) of the players table.

After this, run the following command

Above command will make a new table and the name of the table will be “players”

Step 4. Making Model and Controller files

Now we need to create model and controller classes.

Make a new command like below

  • Above command will try to create an controller class named “PlayersController.php” and a model called “Player.php”
  • But because model class is not there, it will ask you if you want to create model ? say yes.

See the below snippet

  • When you hit “Enter” after writing yes, system will create two files : controller class named “PlayersController.php” and a model called “Player.php”
  • Now, go to app->Player.php class.

Source code for this file is as the follow

I have added just one line in this model class.

  • Now go to app->Http->Controllers->PlayersController.php

Source lines for “PlayersController.php is as the below

This controller class is like the heart of the tutorial. It contains the functions which will implement CRUD operations.

I will explain these functions in detail later.

Step 5. Defining Routes

Navigate to app->routes->web.php file

Code snippet for this file is as the below

  • Last line is defining the routes for all CRUD operations.

To see the routes in detail, trigger the below command

You will find the output like below

So as you can see that various routes are there with GET or POST requests.

Step 6. Creating Blade View files

Now, we need to code some view files to create look and feel of our project.

Fetching all the records

Let us get all the records from the database.

For this, run your project in browser using below command

In your browser hit the url like “”

  • This URL will call the index() function from the PlayersController.php file, which is as below

  • First line will fetch all the records from the Players table.
  • Then system will call the index.blade.php file and will also pass all the fetched records using compact.
  • We do not have created index.blade.php file till now. So, let’s do that first.
  • First of all, go to app->resources->views directory. Make a new folder named “players” inside views folder.
  • Inside players folder, make a new file and give it a name index.blade.php

Write down the below lines in this file

Look at the very first line. It is extending the “layouts” file. We have not create this file also.

So inside views folder, make a new file and name it like “layout.blade.php” and add the blow code in it

This file will help us to use latest designs of buttons, text fields etc. UI widgets.

Making a new Player

Now it is time make a new player and insert its details into database.

  • index.blade.php file have one link like “Create new player“. When the user click on this, compiler will redirect him to a new page.
  • When user clicks the link, system will call create() method from PlayersController.php file and below is it’s source lines

This function will simply call the create.blade.php file.

Inside players folder, create a new file named create.blade.php

Coding lines for create.blade.php is as the below

  • This page have fields like name, sport , country and a submit button. User can add new player from this page.
  • When the user clicks on submit button (Make Player) , system will call store() function from PlayersController.php file.

Below is the code for store() function

  • First line will make a new object of Player model.
  • Then, second line will insert the name given by user into the object.
  • Similarly, third and fourth lines will insert sport and country in the object.
  • Fifth line will store the information into the database.
  • Last line will redirect the user to home page or index.blade.php

Edit and Delete Players Record

Home page makes a list of players name. This names are links.

When the user clicks on any name, system will call the show() function from controller class.

Below are the source lines for show() function

This function will fetch the details of players from the id and then it will call show.blade.php file.

Make a new file (show.blade.php) under resources->views->players directory and below lines in it

  • Above file will display the information about player.
  • One button named “Edit Player” is there. When the user clicks it, system will run edit() function from controller class.

Below is the source for edit() function

  • This function will open edit.blade.php file. Create a new file in resources->views->players directory called edit.blade.php

Write the below lines in edit.blade.php

  • In this file, user can change the information like name, sport and country.
  • There are two buttons in this file : UPDATE PLAYER and DELETE PLAYER

When the user clicks UPDATE PLAYER compiler will run update() function from controller.

  • Above function will update the record in the database and then it will redirect to home page.
  • When the user clicks the DELETE PLAYER , compiler will run destroy() function.

Following is it’s coding lines

  • It will delete the record using id and then will redirect to the home page.

So, it was all for laravel 5.8 CRUD example.

Download Source Code

Click to download laravel 5.8 CRUD code

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 *