In this tutorial we are going to create laravel application with CRUD operation. In programming, create, read, update, and delete are the four basic operations of data storage. We will give you simple example of how to create crud in laravel 8. Laravel is one of the most popular PHP-based framework for creating database-driven apps.
Laravel is described by its creators as the framework for web artisans. It's based on the MVC (Model-View-Controller) pattern and can be used for easily creating apps for making CRUD (Create, Retrieve/Read, Update, Delete) operations on a database.
So, let's create laravel application to perform CRUD operation step by step.
Let's begin to create our laravel application with Jetstream authentication. First of all create new project.
You can create fresh laravel application with one of the below commands:
Replace Application_name with your actual project name.
Let's configure database for our application. If you have don't have any database then create a new one. After creating database open .env file from root directory of your project. if .env file is missing from project then copy content from .env.example and create file. .env file defines many common environment variables
Replace * with your actual configuration in upper code.
Before creating model and migrations let's understand what is model and migrations.
In Laravel, Model is a class that represents the logical structure and relationship of underlying database table. In Laravel, each of the database table has a corresponding Model that allow us to interact with that table. Models gives you the way to retrieve, insert, and update information into your data table. All of the Laravel Models are stored in the main app directory.
Migrations are like version control for your database, allowing your team to define and share the application's database schema definition.
The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Typically, migrations will use this facade to create and modify database tables and columns.
For creating model and migration enter below command to terminal :
This will create a Contact model and a migration file. In the terminal, we get an output similar to:
Open the app/models/Product.php file and update it accordingly:
Open the database/migrations/xxxxxx_create_contacts_table migration file and update it accordingly:
We added the name, description and price fields in the products table structure.
After defining our table structure, we have to create table into database using below command :
Controllers are meant to group associated request handling logic within a single class. In your Laravel project, they are stored in the app/Http/Controllers' directory. The full form of MVC is Model View Controller, which act as directing traffic among the Views and the Models. After creating the model and migrated our database. Let’s now create the controller for working with the Product model.
In your terminal, run the following command:
This command will generate a controller at app/Http/Controllers/ProductController.php and update it accordingly :
Route is a way of creating a request URL of your application. These URL do not have to map to specific files on a website. The best thing about these URL is that they are both human readable and SEO friendly.
We have already create method to handle CRUD for products table. Now we need to provide implementations for these methods at specific URL. For that we have to create route for particular functions. In laravel, All web routes are must be added into particular file routes/web.php.although you can create custom route file and bind it to your application.
In this example, we are going to use default route file so Open the routes/web.php file and update it accordingly
These route binds functions to specific URL which is listed below:
View performs all frontend functionality of our application like displaying data, getting user input or getting requests. In this example, we are going to perform CRUD into single view you can use as many views as you want.
For Creating views, you can create file with .blade.php extension at resources/views/ directory. For this Example, Create index.blade.php file at that directory and alter as below :
For Running or Testing CRUD application enter below command :
It will produce output like below :
Open open your browser and go to http://127.0.0.1:8000/products and test your application.
Ask anything about this examples