🎯 Learning Objectives
- Implement student registration form
- Create course management forms
- Display students in table
- Show individual student details
- Build dashboard with statistics
🗄️ Step 1: Define Migrations
Students Table Migration:
<?php
// database/migrations/xxxx_create_students_table.php
public function up(): void
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('phone');
$table->date('date_of_birth')->nullable();
$table->text('address')->nullable();
$table->timestamps();
});
}
Courses Table Migration:
<?php
// database/migrations/xxxx_create_courses_table.php
public function up(): void
{
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code')->unique();
$table->text('description')->nullable();
$table->integer('duration_weeks');
$table->timestamps();
});
}
Run Migrations:
php artisan migrate
📦 Step 2: Configure Models
Student Model:
<?php
// app/Models/Student.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $fillable = [
'name',
'email',
'phone',
'date_of_birth',
'address'
];
// Relationship: Student has many courses
public function courses()
{
return $this->belongsToMany(Course::class);
}
}
?>
Course Model:
<?php
// app/Models/Course.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $fillable = [
'name',
'code',
'description',
'duration_weeks'
];
// Relationship: Course has many students
public function students()
{
return $this->belongsToMany(Student::class);
}
}
?>
🎮 Step 3: Student Controller
<?php
// app/Http/Controllers/StudentController.php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
// Display all students
public function index()
{
$students = Student::orderBy('created_at', 'desc')->get();
return view('students.index', compact('students'));
}
// Show create form
public function create()
{
return view('students.create');
}
// Store new student
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:100',
'email' => 'required|email|unique:students,email',
'phone' => 'required|string|max:20',
'date_of_birth' => 'nullable|date',
'address' => 'nullable|string|max:500'
]);
Student::create($validated);
return redirect()->route('students.index')
->with('success', 'Student added successfully!');
}
// Show single student
public function show($id)
{
$student = Student::with('courses')->findOrFail($id);
return view('students.show', compact('student'));
}
}
?>
🎨 Step 4: Create Master Layout
<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title', 'Student Management System')</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.navbar { background: linear-gradient(135deg, #ff6e7f 0%, #bfe9ff 100%); }
.card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); margin-bottom: 20px; }
.btn-primary { background: #ff6e7f; border: none; }
.btn-primary:hover { background: #ff5569; }
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container">
<a class="navbar-brand" href="/">🎓 Student Management</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="{{ route('dashboard') }}">Dashboard</a></li>
<li class="nav-item"><a class="nav-link" href="{{ route('students.index') }}">Students</a></li>
<li class="nav-item"><a class="nav-link" href="{{ route('courses.index') }}">Courses</a></li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
@if(session('success'))
<div class="alert alert-success alert-dismissible fade show">
{{ session('success') }}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
@endif
@yield('content')
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
📝 Step 5: Create Form View
<!-- resources/views/students/create.blade.php -->
@extends('layouts.app')
@section('title', 'Add New Student')
@section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h3>Add New Student</h3>
</div>
<div class="card-body">
<form action="{{ route('students.store') }}" method="POST">
@csrf
<div class="mb-3">
<label class="form-label">Full Name *</label>
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
value="{{ old('name') }}" required>
@error('name')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Email Address *</label>
<input type="email" name="email" class="form-control @error('email') is-invalid @enderror"
value="{{ old('email') }}" required>
@error('email')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Phone Number *</label>
<input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror"
value="{{ old('phone') }}" required>
@error('phone')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="mb-3">
<label class="form-label">Date of Birth</label>
<input type="date" name="date_of_birth" class="form-control" value="{{ old('date_of_birth') }}">
</div>
<div class="mb-3">
<label class="form-label">Address</label>
<textarea name="address" class="form-control" rows="3">{{ old('address') }}</textarea>
</div>
<div class="d-flex justify-content-between">
<a href="{{ route('students.index') }}" class="btn btn-secondary">Cancel</a>
<button type="submit" class="btn btn-primary">Add Student</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
📊 Step 6: Students List View
<!-- resources/views/students/index.blade.php -->
@extends('layouts.app')
@section('title', 'All Students')
@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>All Students</h2>
<a href="{{ route('students.create') }}" class="btn btn-primary">+ Add New Student</a>
</div>
<div class="card">
<div class="card-body">
@if($students->count() > 0)
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Registered</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->phone }}</td>
<td>{{ $student->created_at->format('M d, Y') }}</td>
<td>
<a href="{{ route('students.show', $student->id) }}" class="btn btn-sm btn-info">View</a>
<a href="{{ route('students.edit', $student->id) }}" class="btn btn-sm btn-warning">Edit</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
<div class="alert alert-info">
No students found. <a href="{{ route('students.create') }}">Add your first student</a>
</div>
@endif
</div>
</div>
@endsection
👁️ Step 7: Student Details View
<!-- resources/views/students/show.blade.php -->
@extends('layouts.app')
@section('title', 'Student Details')
@section('content')
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h3>Student Information</h3>
</div>
<div class="card-body">
<table class="table">
<tr>
<th width="30%">Student ID:</th>
<td>{{ $student->id }}</td>
</tr>
<tr>
<th>Name:</th>
<td>{{ $student->name }}</td>
</tr>
<tr>
<th>Email:</th>
<td>{{ $student->email }}</td>
</tr>
<tr>
<th>Phone:</th>
<td>{{ $student->phone }}</td>
</tr>
<tr>
<th>Date of Birth:</th>
<td>{{ $student->date_of_birth ?? 'Not provided' }}</td>
</tr>
<tr>
<th>Address:</th>
<td>{{ $student->address ?? 'Not provided' }}</td>
</tr>
<tr>
<th>Registered:</th>
<td>{{ $student->created_at->format('F d, Y') }}</td>
</tr>
</table>
<div class="mt-4">
<a href="{{ route('students.index') }}" class="btn btn-secondary">Back to List</a>
<a href="{{ route('students.edit', $student->id) }}" class="btn btn-warning">Edit Student</a>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header">
<h5>Enrolled Courses</h5>
</div>
<div class="card-body">
@if($student->courses->count() > 0)
<ul class="list-group">
@foreach($student->courses as $course)
<li class="list-group-item">{{ $course->name }}</li>
@endforeach
</ul>
@else
<p class="text-muted">Not enrolled in any courses yet.</p>
@endif
</div>
</div>
</div>
</div>
@endsection
🏠 Step 8: Dashboard
<?php
// app/Http/Controllers/DashboardController.php
namespace App\Http\Controllers;
use App\Models\Student;
use App\Models\Course;
class DashboardController extends Controller
{
public function index()
{
$totalStudents = Student::count();
$totalCourses = Course::count();
$recentStudents = Student::latest()->limit(5)->get();
return view('dashboard', compact('totalStudents', 'totalCourses', 'recentStudents'));
}
}
?>
<!-- resources/views/dashboard.blade.php -->
@extends('layouts.app')
@section('title', 'Dashboard')
@section('content')
<h2 class="mb-4">Dashboard</h2>
<div class="row">
<div class="col-md-4">
<div class="card text-white bg-primary">
<div class="card-body">
<h5 class="card-title">Total Students</h5>
<h2>{{ $totalStudents }}</h2>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card text-white bg-success">
<div class="card-body">
<h5 class="card-title">Total Courses</h5>
<h2>{{ $totalCourses }}</h2>
</div>
</div>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h5>Recent Students</h5>
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Registered</th>
</tr>
</thead>
<tbody>
@foreach($recentStudents as $student)
<tr>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->created_at->diffForHumans() }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endsection
✏️ Practice Exercises
- Create similar CRUD for Courses (create.blade.php, index.blade.php)
- Add search functionality to students list
- Implement pagination for student list
- Add student profile photo upload
- Create a statistics card showing average age
✅ Today's Deliverable
- ✓ Student registration form working
- ✓ Students list displaying correctly
- ✓ View individual student details
- ✓ Bootstrap styling applied
- ✓ Dashboard showing statistics
- ✓ Flash messages for success