Laravel 8 CRUD Example | Laravel 8 Tutorial

Laravel 8 CRUD Example | Laravel 8 Tutorial

Laravel 8 CRUD Tutorial: Manage Student Records

Are you looking to build a simple yet powerful CRUD (Create, Read, Update, Delete) application in Laravel 8? In this tutorial, we'll walk through how to create a Laravel 8 app to manage student records with full functionality: create, list, update, and delete.

Whether you're a beginner or brushing up on your Laravel skills, this guide is for you!

Step 1: Install Laravel 8 Project

Start by creating a fresh Laravel 8 project using Composer:

composer create-project --prefer-dist laravel/laravel student-crud

Step 2: Configure Database

Update your .env file with your database credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=student_db DB_USERNAME=root DB_PASSWORD=admin

Make sure to create the database (student_db) using phpMyAdmin or MySQL CLI.

Step 3: Create Student Model and Migration

Run this command:

php artisan make:model Student -m

Then open the migration file in database/migrations/ and update it like this:

Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('last_name'); $table->text('address'); $table->timestamps(); });

Now run the migration:

php artisan migrate

Step 4: Define Fillable Fields

In app/Models/Student.php, add:

protected $fillable = ['first_name', 'last_name', 'address'];

Step 5: Setup Routes

Add these routes to your routes/web.php:

use App\Http\Controllers\StudentController; Route::get('students/create', [StudentController::class, 'create'])->name('student.create'); Route::post('students', [StudentController::class, 'store'])->name('student.store'); Route::get('students', [StudentController::class, 'index'])->name('student.list'); Route::get('students/{student}', [StudentController::class, 'show'])->name('student.show'); Route::get('students/{student}/edit', [StudentController::class, 'edit'])->name('student.edit'); Route::patch('students/{student}', [StudentController::class, 'update'])->name('student.update'); Route::delete('students/{student}', [StudentController::class, 'destroy'])->name('student.destroy');

Step 6: Create Student Controller

Generate the controller:

php artisan make:controller StudentController --resource

Update StudentController.php:

public function index() { $students = Student::all(); return view('student.list', compact('students')); } public function create() { return view('student.create'); } public function store(Request $request) { $request->validate([ 'txtFirstName' => 'required', 'txtLastName' => 'required', 'txtAddress' => 'required' ]); Student::create([ 'first_name' => $request->txtFirstName, 'last_name' => $request->txtLastName, 'address' => $request->txtAddress, ]); return redirect()->route('student.create')->with('success', 'Student added successfully!'); } public function show(Student $student) { return view('student.view', compact('student')); } public function edit(Student $student) { return view('student.edit', compact('student')); } public function update(Request $request, Student $student) { $request->validate([ 'txtFirstName' => 'required', 'txtLastName' => 'required', 'txtAddress' => 'required' ]); $student->update([ 'first_name' => $request->txtFirstName, 'last_name' => $request->txtLastName, 'address' => $request->txtAddress, ]); return redirect()->route('student.list')->with('success', 'Student updated successfully!'); } public function destroy(Student $student) { $student->delete(); return redirect()->route('student.list')->with('success', 'Student deleted successfully!'); }

Step 7: Create Blade Views

Layout: resources/views/layout/app.blade.php

<!DOCTYPE html> <html> <head> <title>Laravel 8 Student CRUD</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container mt-4"> @yield('content') </div> </body> </html>

Add Student Form: student/create.blade.php

@extends('layout.app') @section('content') <h2>Add New Student</h2> @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif <form action="{{ route('student.store') }}" method="POST"> @csrf <div class="form-group"> <label>First Name:</label> <input type="text" class="form-control" name="txtFirstName"> </div> <div class="form-group"> <label>Last Name:</label> <input type="text" class="form-control" name="txtLastName"> </div> <div class="form-group"> <label>Address:</label> <textarea class="form-control" name="txtAddress"></textarea> </div> <button type="submit" class="btn btn-success">Submit</button> </form> @endsection

Student List: student/list.blade.php

@extends('layout.app') @section('content') <h2>Student List</h2> <a href="{{ route('student.create') }}" class="btn btn-success mb-3">Add Student</a> @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif <table class="table table-bordered"> <thead> <tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Address</th><th>Actions</th></tr> </thead> <tbody> @foreach ($students as $student) <tr> <td>{{ $student->id }}</td> <td>{{ $student->first_name }}</td> <td>{{ $student->last_name }}</td> <td>{{ $student->address }}</td> <td> <a href="{{ route('student.edit', $student->id) }}" class="btn btn-primary btn-sm">Edit</a> <form action="{{ route('student.destroy', $student->id) }}" method="POST" style="display:inline-block;"> @csrf @method('DELETE') <button type="submit" class="btn btn-danger btn-sm">Delete</button> </form> </td> </tr> @endforeach </tbody> </table> @endsection

Edit Student: student/edit.blade.php

@extends('layout.app') @section('content') <h2>Edit Student</h2> <form method="POST" action="{{ route('student.update', $student->id) }}"> @csrf @method('PATCH') <div class="form-group"> <label>First Name:</label> <input type="text" class="form-control" name="txtFirstName" value="{{ $student->first_name }}"> </div> <div class="form-group"> <label>Last Name:</label> <input type="text" class="form-control" name="txtLastName" value="{{ $student->last_name }}"> </div> <div class="form-group"> <label>Address:</label> <textarea class="form-control" name="txtAddress">{{ $student->address }}</textarea> </div> <button type="submit" class="btn btn-success">Update</button> </form> @endsection

View Student: student/view.blade.php

@extends('layout.app') @section('content') <h2>Student Details</h2> <table class="table table-bordered"> <tr><th>First Name:</th><td>{{ $student->first_name }}</td></tr> <tr><th>Last Name:</th><td>{{ $student->last_name }}</td></tr> <tr><th>Address:</th><td>{{ $student->address }}</td></tr> </table> @endsection

Step 8: Run the Application

php artisan serve

Then open your browser and navigate to:

http://localhost:8000/students/create

Conclusion

That’s it! You’ve successfully built a complete Laravel 8 CRUD app to manage student records. You learned how to:

  • Configure database settings

  • Create migrations and models

  • Use resource controllers

  • Build dynamic Blade views

  • Perform create, read, update, and delete operations

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. Unknown
    Unknown
    Hi there,
    first of all thanxs for this nice and quick tutorial. But unfortunately I wasn´t able to coomplete it and see the result. I guess that you probably made some little errors and typos while doing this. Like instruction reagrding PostController vs-StudentController and the other things is this:
    Step 7: Create Blade Files
    Here in this step, we create our view files. First, create two folders one is “layout” and another is “posts”. In the layout, folder creates an app.blade.php file, and inside the posts folder create another blade file. If you don’t know to integrate our blade files in Laravel then please check first here how to integrate the Laravel bootstrap admin panel. Now create below blade files for your crud app:


    layout and posts folder doe go where.


    --> Would be very happy if you could check this and reply&correct these.

    Thanxs :)
close