Home > Laravel > Create Laravel 9 CRUD Application Step by Step

Create Laravel 9 CRUD Application Step by Step

In this tutorial, we will learn about how to create a CRUD application in laravel 9 with step by step guide. We will see how to make create, read, update and delete requests in laravel.

If you are a beginner then this tutorial is for you to learn the first step of laravel framework. It is easy to learn if you have knowledge of PHP and you can easily create laravel application with the latest version of 9.

So in this tutorial example, we will make laravel 9 CRUD application. First, we will create a “posts” table using migration and then will make a controller and model for “posts” and then routes files in the web.php file.

So let’s get started on making a CRUD application with laravel latest version 9.

Install Laravel 9

To install the laravel 9, you have to run the following command. It will download the laravel and required dependencies for you in your project folder. If you have already created the project then you skip this step.

Note: The --prefer-dist flag will install the latest version of laravel. At this time I have installed the laravel 9.1 version. It may be different when you follow this. The “codeblog” is the project name. For more detail check the laravel installation tutorial.

Database Configuration

After installing the laravel, we have to set up the database settings. To configure the database connection in laravel, open the .env file in the project root directory.

In the .env file, you will see all the global environment variables here. Find the database-related variables and set up the required fields.

See the following lines of laravel database configuration for reference.

Create Migration

Now we will create a migration file to set up tables column names and their types. After setup the migration file we will run the migration to create the database table according to the migration file.

Run the following command to create the migration file for the “posts” table.

Now you can see this migration file in the “database/migrations” directory. Open that file and make the changes in the up() function for column names and types. See the following reference code.

After making the changes in the above file, run the following command to migrate the migration. It will create a “posts” table in your database.

Create Model and Controller

Now it’s time to create the model and controller to interact with the “posts” table. The model is used to interact with the table and the controller is used for making functionality.

We will create a model and controller together in one command. So, run the following command to create the model and controller in laravel.

The --resource flag is used to create the boilerplate of the controller file. And the --model flag is used for creating the Post model along with the controller.

app/Models/Post.php

app/Http/Controllers/PostController.php

In the above both files model and controller, you can customize them according to your project or database table.

Add Resource Route

To run the application we have to define routes that will point to the endpoint of request and show the result accordingly.

In laravel, we will add the resource routes on the web.php file in the /routes folder (routes/web.php), so simply take a reference of the below code and add the resource routes of your application.

Note: Make sure you also have imported the controller at the top.

Create Blade (View) Files

So, after everything is done, now you need to make blade files to show the fronted interface of your application. We will make blade view files in /resources/views folder.

In the /views folder, create a new folder called “posts” and create all the following files one by one in this folder.

layout.blade.php

In this file, we will create a global common layout for our laravel crud application. We will define the header and footer in this file and take a div container class in the body with dynamically content using the @yield.

index.blade.php

This file will use to show the list of all posts. We will loop through each post by posts array taken from the controller.

create.blade.php

In this file, we will make a form to store the data fields in the database for the post table. And also check for validation rules that we have defined in the controller and show them if any occur.

edit.blade.php

This blade file we will use to update the data fields. We will show the form with populated the saved values of fields. And here you can update the post data field by submitting the new value.

show.blade.php

In this file, we will show the single post details fetching by post ID. You can design it as you want, for this tutorial I’m going with a table to show the details.

So these are the blade templates of our CRUD application, you can take the file name as you want, you can customize them, and design them according to your requirements.

Note: Did you notice something in the above blade templates? We have used the @extend and @section placeholder.

The @extend import the main app layout of the application in the child views. And @section will highlight that this section is for the content section in the main layout.

Run Laravel Application

If you have successfully done all the above steps, so now it’s time to run our laravel 9 CRUD application. To run the laravel application, you need to run the following command in the terminal.

When you hit the above command, it will start to serve your laravel application in your browser or give you a link with a port to check your application.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment