Learn to build modern web applications with Laravel 9 by following this comprehensive CRUD tutorial. From setting up a new project to creating, reading, updating, and deleting records, this Laravel 9 CRUD Tutorial covers all the essential skills you need to master in order to create dynamic and functional web apps with Laravel 9. Whether you’re a beginner or an experienced developer, this tutorial is the perfect guide for building CRUD applications in Laravel 9.
What is Laravel crud?
Laravel CRUD refers to the basic operations that can be performed on a database table in a web application, such as Create, Read, Update and Delete. These operations are often referred to as CRUD
Which is an acronym for the four basic types of database operations. Laravel is a powerful PHP framework that makes it easy to build web applications and handle these CRUD operations. With Laravel
Developers can easily create and manage database tables, perform basic CRUD operations, and build complex and dynamic web applications with ease.
Laravel 9 CRUD Tutorial: How to build a Laravel Crud application?
Laravel is a powerful PHP framework that makes it easy to build modern web applications. In this tutorial, we will be covering the basics of creating a CRUD (Create, Read, Update, and Delete) application using Laravel 9.
Related Articles : Laravel Tutorial: How to send Email in Laravel
Before we begin, it is important to note that in order to follow along with this tutorial, you will need to have a basic understanding of PHP and some experience with the Laravel framework.
First, let’s start by creating a new Laravel project. In your terminal, navigate to the directory where you want to create the project and run the following command:
composer create-project --prefer-dist laravel/laravel my-project
Create Database
This will create a new Laravel project in a directory named “my-project”.
Next, we need to create a new database using Apache client or Xampp, Run your local host client and go to http://localhost/phpmyadmin
Now set up the connection in our project. In your .env file, set the following:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my-project
DB_USERNAME=root
DB_PASSWORD=
Be sure to replace the values with the appropriate information for your local development environment.
Create Model and Controller
Now, let’s create a new model and migration for our CRUD application. In your terminal, navigate to your project’s root directory and run the following command:
php artisan make:model Item -m
This will create a new model named “Item” and a corresponding migration file. In the migration file, you can define the columns for the table that will be created in the database.
Finally, we can create the controller and views for our CRUD application. In your terminal, run the following command:
php artisan make:controller ItemController --resource
This will create a new controller named “ItemController” with all of the necessary CRUD actions.
With this basic setup, you can now start building out your CRUD application using Laravel 9. Some next steps could include adding validation and form handling, and creating views for displaying and editing data.
Note: It is important to note that this is just a basic overview of creating a CRUD application using Laravel 9. There are many other features and tools available in the framework that can be used to build more complex and robust applications.
Create the view
Next, let’s go ahead and create the views for our CRUD application. In the “resources/views"
directory, create a new folder called items
and within that folder create four new files called
“create.blade.php
, edit.blade.php
, index.blade.php
, and show.blade.php
“
These files will correspond to the create
, edit
, index
, and show
actions in our controller.
For example, in the create.blade.php
file, we can add a form for creating a new item:
<form action="{{route('items.store')}}" method="post">
@csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" name="description" id="description"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Related Articles : Laravel Tutorials: Task scheduling
In the “store"
method of the “ItemController
” we can handle the form data and create a new item in the database:
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'description' => 'required',
]);
$item = Item::create($validatedData);
return redirect()->route('items.index')->with('success','Item created successfully.');
}
Similarly, in the “edit.blade.php"
file, we can add a form for editing an existing item:
<form action="{{route('items.update', $item->id)}}" method="post">
@csrf
@method('PUT')
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" value="{{$item->name}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" name="description" id="description">{{$item->description}}</textarea>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
The “update"
method of the controller will handle the form data and update the item in the database.
public function update(Request $request, Item $item)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'description' => 'required',
]);
$item->update($validatedData);
return redirect()->route('items.index')->with('success','Item updated successfully.');
}
The “index.blade.php
” file will display a list of all items in the database, while the "show.blade.php
” file will display the details of a single item.
In the “index.blade.php
” file, we can use a foreach
loop to iterate through all the items and display them in a table:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{$item->name}}</td>
<td>{{$item->description}}</td>
<td>
<a href="{{route('items.show', $item->id)}}" class="btn btn-info">Show</a>
<a href="{{route('items.edit', $item->id)}}" class="btn btn-primary">Edit</a>
<form action="{{route('items.destroy', $item->id)}}" method="post">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
And in the “show.blade.php
” file, we can display the details of a single item:
<div class="card">
<div class="card-body">
<h5 class="card-title">{{$item->name}}</h5>
<p class="card-text">{{$item->description}}</p>
</div>
</div>
Finally, don’t forget to include the routes for these views in the “web.php
” file.
Route::resource('items', 'ItemController');
In Laravel, resources routes are automatically generated for a controller when you use the php artisan make:controller --resource
command. These routes provide a convenient way to handle CRUD operations for a specific resource, such as a database table.
Once you’ve created your controller and defined the routes, you can access the different routes by visiting the corresponding URLs in your browser. For example:
- To access the
index
action of your controller, you would visithttp://localhost/items
( assuming items is your resource name ) - To access the
create
action of your controller, you would visithttp://localhost/items/create
- To access the
store
action of your controller, you would submit a form tohttp://localhost/items
using the HTTP methodPOST
- To access the
show
action of your controller, you would visithttp://localhost/items/{id}
where{id}
is the id of the item you want to show - To access the
edit
action of your controller, you would visithttp://localhost/items/{id}/edit
where{id}
is the id of the item you want to edit - To access the
update
action of your controller, you would submit a form tohttp://localhost/items/{id}
using the HTTP methodPUT
orPATCH
- To access the
destroy
action of your controller, you would submit a form tohttp://localhost/items/{id}
using the HTTP methodDELETE
You can also use the route()
function in your views to generate URLs for these routes. For example:
<a href="{{ route('items.index') }}">View All Items</a>
It’s worth noting that you may want to use a web middleware group in your route definition to protect your routes and make sure that only authenticated users can access them.
Route::middleware(['web'])->group(function(){
Route::resource('items', 'ItemController');
});
By this way, you can access the resources routes in your Laravel application and perform the CRUD operations in your resources.
With this setup, we have a basic CRUD application using Laravel 9. You can now easily create, read, update and delete items in the database. You can also add other functionality, like handling file uploads, pagination, and more advanced validation.
Laravel 9 provide a lot of useful tools and features to make building CRUD applications easy and efficient. This tutorial should have given you a good starting point for creating your own CRUD application using Laravel 9.