Laravel 9 CRUD Example Tutorial | Laravel 9 CRUD for Beginners
In this article we have to learn about the Laravel 9 CRUD (create, read, update and delete) operation. The CRUD operation in laravel 9 step by step, we have provided the tutorials for the beginners and advanced level. In this article I have explained to you how to validate add and update forms in Laravel 9 step by step on server-side. So let’s see how to create the CRUD application in Laravel 9.
I have more than experience in this field and I have completed more than 20 projects on Laravel. Therefore I will share with you my best experience on how to use CRUD operation in Laravel 9. So that you will understand how to develop the best Laravel app.
In this tutorial we have learned step by step how to create the student module using crud operation in Laravel 9 with form validation. How to insert , read, update, and delete operations in Laravel 9 app.
Following these steps to create a crud operation app in Laravel 9.
- How to set up a Laravel project.
- Configure database connection in Laravel 9.
- Create Student Model & Migration For CRUD App
- Create a Student Controller by using the Artisan command
- Create Route
- Create Blade Views File
- Create Directory Name Student
- List.blade.php
- Create.blade.php
- Edit.blade.php
- Validate form submit record from controller.
- Run Laravel CRUD App on Development Server
1. How to set up a Laravel project
In this setup, we will learn how to download or install Laravel 9 project setup in the localsystem. So follow the instructions on how to install.
First of all open your terminal and move your directory. That means where you want to create the laravel project. After selecting your directory following the command to install the new laravel app into your machine.
composer create-project laravel/laravel StudentRegister --prefer-dist
After running this command in your terminal you have to see output when your command is successfully done.
If you want to know more about Laravel 9 installation. I have provided the link in the bottom so that you will better understand about the laravel 9 installation.
2. Configure database connection in Laravel 9
Let’s set up the database configure to connect to the project from the PhpMyAdmin server.
Open your downloaded app folder studentRegister. After that, open the .env file. After that replace the username, password and database name as you have created in the PhpMyAdmin.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password
3. Create Student Model & Migration For CRUD App
In this step, we will learn how to create models and migration files with a single command.
Let’s open your terminal again and run the below command. Please make sure you are selected in your studentRegister directory.
php artisan make:model Student -m
After running this command. It will create two files: the first is the migrations file and second is the model file.
Open migrations file. It’s created under the database/migrations folder. Let's see what it looks like.
Now you have to see the migrations file created under the migrations folder. Open your migrations file and replace the up() function. up() function basically defined for the crate table column name. So let’s see how to create a column in the database table in the migration file.
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('phone_number')->nullable();
$table->enum('gender',['1','2'])->comment('1=>Male, 2=>Female');
$table->string('date_of_birth')->nullable();
$table->string('state');
$table->timestamps();
});
}
Then open again your terminal after the run the below command in your terminal. When you run this command in your terminal. Then it will automatically create the column under the students table.
php artisan migrate
After that open your model file and define the column name so that you easily access students table column values using model class.
Let’s open app\Models\Student.php and replace the code in the below code.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
use HasFactory;
protected $fillable = ['first_name','last_name','email','phone_number','gender','date_of_birth','state'];
}
After changing your model file, save your file.
3. Create a Student Controller by using the Artisan command.
In this article we will learn how to create a student controller file using artisan command in Laravel 9 app. Laravel provides an easy to create controller resource file with a second using the command. So let’s see how to create, open again your terminal and write the command.
php artisan make:controller StudentRegisterController --resource
After running this command. When you go to your controller folder you have to see the StudentRegisterController.php file is created.
4. Create Route
Hello friend. I hope you will enjoy this article, so let’s see how to define the route url for accessing the controller file. After that the controller calls the views template.
Let’s open your web.php file under the Route folder. After that add the below line for access the controller urls.
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentRegisterController;
Route::resource('student', StudentRegisterController::class);
5. Create Blade Views File
Create the student directory inside of the resources/views folder. After that, create some blade files. See following:
- List.blade.php
- Create.blade.php
- Edit.blade.php
- Show.blade.php
Note that, create list.blade.php, create.blade.php and edit.blade.php inside of the student directory. After that update the following code into following files:
Let’s create a list.blade.php file inside of the student views directory.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 9 CRUD Tutorial Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 9 CRUD Example Tutorial</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" href="{{ route('student.create') }}"> Student</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>S.No</th>
<th>Student Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Gender</th>
<th>Dob</th>
<th width="280px">Action</th>
</tr>
@foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->first_name }} {{$student->last_name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->phone_number }}</td>
<td>{{ ($student->gender == 1 ? 'Male': 'Female') }}</td>
<td>{{ $student->date_of_birth }}</td>
<td>
<form action="{{ route('student.destroy',$student->id) }}" method="Post">
<a class="btn btn-primary" href="{{ route('student.show',$student->id) }}">show</a>
<a class="btn btn-primary" href="{{ route('student.edit',$student->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $students->links() !!}
</body>
</html>
Let’s create a create.blade.php file inside of the student views directory.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Student Form - Laravel 9 CRUD</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>Add Student</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('student.index') }}"> Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif
<form action="{{ route('student.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>First Name:</strong>
<input type="text" name="first_name" class="form-control" placeholder="First Name">
@error('first_name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Last Name:</strong>
<input type="text" name="last_name" class="form-control" placeholder="Last Name">
@error('last_name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Student Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Student Email">
@error('email')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Phone:</strong>
<input type="number" name="phone_number" class="form-control" placeholder="Enter Phone">
@error('phone_number')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>DOB:</strong>
<input type="date" name="date_of_birth" class="form-control" placeholder="Enter dob">
@error('date_of_birth')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Gender:</strong>
<label> <input type="radio" name="gender" value="1" >Male</lable>
<label> <input type="radio" name="gender" value="2">Female</lable>
@error('gender')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>State:</strong>
<select name="state" class="form-control">
<option value="">Select State</option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam ">Assam </option>
</select>
@error('state')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<button type="submit" class="btn btn-primary ml-3">Submit</button>
</div>
</div>
</form>
</body>
</html>
Let’s create a edit.blade.php file inside of the student views directory
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Update Student Form - Laravel 9 CRUD</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>Update Student</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('student.index') }}"> Back</a>
</div>
</div>
</div>
@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif
<form action="{{ route('student.update',$student->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>First Name:</strong>
<input type="text" name="first_name" class="form-control" placeholder="First Name" value="<?php echo $student->first_name; ?>">
@error('first_name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Last Name:</strong>
<input type="text" name="last_name" class="form-control" placeholder="Last Name" value="<?php echo $student->last_name; ?>" >
@error('last_name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Student Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Student Email" value="<?php echo $student->email; ?>">
@error('email')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Phone:</strong>
<input type="number" name="phone_number" class="form-control" placeholder="Enter Phone" value="<?php echo $student->phone_number; ?>" >
@error('phone_number')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>DOB:</strong>
<input type="date" name="date_of_birth" class="form-control" placeholder="Enter dob" value="<?php echo $student->date_of_birth; ?>" >
@error('date_of_birth')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Gender:</strong>
<label> <input type="radio" name="gender" value="1" <?php echo ($student->gender == '1' ? 'checked':''); ?> >Male</label>
<label> <input type="radio" name="gender" value="2" <?php echo ($student->gender == '2' ? 'checked':''); ?> >Female</label>
@error('gender')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>State:</strong>
<select name="state" class="form-control">
<option value="">Select State</option>
<option value="Andhra Pradesh" <?php echo ($student->state == 'Andhra Pradesh' ? 'selected':''); ?> >Andhra Pradesh</option>
<option value="Arunachal Pradesh" <?php echo ($student->state == 'Arunachal Pradesh' ? 'selected':''); ?> >Arunachal Pradesh</option>
<option value="Assam" <?php echo ($student->state == 'Assam' ? 'selected':''); ?>>Assam </option>
</select>
@error('state')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<button type="submit" class="btn btn-primary ml-3">Update</button>
</div>
</div>
</form>
</body>
</html>
Let’s create a show.blade.php file inside of the student views directory
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 9 CRUD Tutorial Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 9 CRUD Example Tutorial</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-info" href="{{ route('student.index') }}"> Back</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>S.No</th>
<th>Student Name</th>
<th>Email</th>
<th>Phone Number</th>
<th>Gender</th>
<th>Dob</th>
<th>State</th>
</tr>
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->first_name }} {{$student->last_name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->phone_number }}</td>
<td>{{ ($student->gender == 1 ? 'Male': 'Female') }}</td>
<td>{{ $student->date_of_birth }}</td>
<td>{{ $student->state }}</td>
</tr>
</table>
</body>
</html>
6. Validate form submit record from controller.
In this step, we will learn how to validate Laravel crud form. After submitting from the machine. So let’s open your StudentRegisterController.php file inside of the App\Http\Controllers folder. Copy the below code and paste your studenRegisterController.php file.
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentRegisterController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data['students'] = Student::orderBy('id','desc')->paginate(10);
return view('student.list', $data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('student.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
'phone_number' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'state' => 'required'
]);
Student::create($request->all());
return redirect()->route('student.index')->with('success','student has been created successfully.');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$student = Student::find($id);
return view('student.show',compact('student'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$student = Student::find($id);
return view('student.edit',compact('student'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
'phone_number' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'state' => 'required'
]);
$student = Student::find($id);
$student->update($request->all());
return redirect()->route('student.index')->with('success','student has been updated successfully.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Student::find($id)->delete();
return redirect()->route('student.index')->with('success','Company has been deleted successfully');
}
}
If you submit the add or edit form blank. So the error message will be displayed with the help of the code given below:
@error('name')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
8. Run Laravel CRUD App on Development Server
Last step, open command prompt and run the following command to start development server:
php artisan serve
Then open your browser and copy and paste in browser url after that hit the following url on it:
http://127.0.0.1:8000/student
Conclusion-:
In this article, we have covered how to set up the Laravel project. After that we have learned about how to configure databases in Laravel 9 application. After that we have covered CRUD operation in laravel 9 step by step.
There are points we have covered.
1. How to set up a Laravel project.
2. Configure database connection in Laravel 9.
3. Create Student Model & Migration For CRUD App
4. Create a Student Controller by using the Artisan command
5. Create Route
6. Create Blade Views File
7. Validate form submit record from controller.
8. Run Laravel CRUD App on Development Server