Laravel 5.7 CRUD example from scratch

Laravel 5.7 CRUD example from scratch

The Laravel 5.7 was just released and is currently under a hot topic. There are many new features in this latest versions. All the new features and upgrades are discussed in detail in this article. In this article, we are going to implement CRUD example from scratch in Laravel 5.7. If you want to read about

There are certain requirements for installing laravel 5.7. Please take a look at the official documentation.

Create Project

To get started, I have started a fresh laravel project. We can install through composer running the following command:

Database Setup

Now, we need to setup our database. Create a new database and fill required credentials in .env file.

Create Model and Migration

After database setup, we need to create migration file. To create a migration, run the following command:

This command creates Article model and migration file. We need to add some columns to store our record. So, after some changes our migration file looks like below:

We need to run the migration after these changes.

Model

Our table is ready. Now, we need to change our model. After some change, our final model looks like below:

Seeding Database

Let’s seed our database. We will use tinker to seed our database. If you want to learn more about tinker, you can read this article How to use Tinker in Laravel Application. First, we create our factory like below:

After this setup, we run our command in tinker shell:

Route

To handle our http request, we need to create our routes. We will use route resources.

Create Controller

To handle our logics, we will create a new controller through artisan command.

We will write some methods to handle our logic and views. Thus, our final controller looks like below:

Create Views

We will create 4 blade files for displaying our all records, showing single record, editing our record and and creating the record.

Index Blade File

This blade file is used to display all our records.

Create Blade File

This blade file is used to display form to create new article.

Show Blade File

This blade is used to display a single article.

Edit Blade File

This blade file is used to edit the article.

Delete Record

To delete our record, we already added our edit function our index blade file.

This is all about coding. Now, let’s test our application. To run the server, simply run the below command:

After that, we can navigate to our index page http://127.0.0.1:8000/articles Then, we can see the articles list. Similarly, you can test going to different routes.

  • http://127.0.0.1:8000/articles
  • http://127.0.0.1:8000/articles/create
  • http://127.0.0.1:8000/articles/1/edit

Thank you for reading this article. If you find any issue or want to give feedback, please leave a comment below.