Student Management System Using Laravel and MYSQL

Rashmi Mishra
0

 



Student Management System 

Using Laravel and MYSQL

Laravel is a popular open-source PHP framework designed for web application development. It follows the Model-View-Controller (MVC) architectural pattern, which helps developers organize their code in a logical and efficient manner.

Key Features of Laravel:

1.  Elegant Syntax: Laravel provides an intuitive and expressive syntax that simplifies common tasks in web development, such as routing, authentication, and caching.

2.  Blade Templating Engine: It includes a lightweight and powerful templating engine called Blade, which allows for dynamic content rendering in views.

3.  Eloquent ORM: Laravel's Object-Relational Mapping (ORM) makes database interactions seamless by allowing developers to work with databases using PHP objects instead of raw SQL.

4.  Routing: It offers a simple yet powerful routing mechanism to define application endpoints and handle HTTP requests.

5.  Middleware: Middleware allows filtering and modifying HTTP requests before they reach your application logic.

6.  Built-in Authentication and Authorization: Laravel simplifies user authentication and access control through pre-built libraries.

7.  Task Scheduling: The framework supports automated scheduling of repetitive tasks using the built-in task scheduler.

8.  Testing Tools: Laravel comes with robust testing tools, making it easier to test applications.

Why Use Laravel?

Laravel emphasizes developer productivity, security, and scalability. It provides a wide range of tools and libraries, making it ideal for developing modern web applications, from small personal projects to large-scale enterprise solutions. Its vibrant community and extensive documentation further support developers in building reliable and maintainable applications.

 

Creating a Student Management System in Laravel with MySQL involves several steps, from setting up the environment to implementing CRUD operations.


Step 1: Install Laravel

1.  Install Composer
Ensure you have Composer installed.

1. Check Composer Installation

Open a terminal or command prompt and type:

composer --version

Expected Output

If Composer is installed, you will see output similar to this:

Composer version 2.x.x 202x-xx-xx

If you see the version, Composer is installed successfully.


2. If Composer is Not Installed

If the command is not recognized, it means Composer is not installed. Follow these steps to install it:

For Windows:

1.  Download the Composer-Setup executable from the official website.

2.  Run the installer and follow the instructions.

3.  Restart your terminal and check the version using:

composer --version

For macOS/Linux:

Run the following commands in your terminal:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php composer-setup.php

php -r "unlink('composer-setup.php');"

Move Composer globally:

sudo mv composer.phar /usr/local/bin/composer

Then verify:

composer --version


3. Verify Laravel Installation

After ensuring Composer is installed, check if Laravel is installed globally:

laravel --version

If Laravel CLI is installed, it will show the version:

Laravel Installer x.x.x

If not, you can install Laravel globally with Composer:

composer global require laravel/installer



Ensure your PATH is updated to include Composer’s global bin directory. Add this to your system’s environment variables if needed:

  • Windows: C:\Users\<YourUserName>\AppData\Roaming\Composer\vendor\bin
  • Linux/MacOS: ~/.composer/vendor/bin

Then verify again:

laravel --version



Create a New Laravel Project  

After installation all required necessary , Create a New Laravel Project using the following command from command line

composer create-project laravel/laravel student-management




cd student-management




2.  Start the Development Server

php artisan serve

Your application will be accessible at http://localhost:8000.






Step 2: Configure the Database

1.  Set Up MySQL
Create a database in MySQL for the project, e.g., student_management.



2.  Update .env File
Modify the .env file to include your database credentials:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=student_management

DB_USERNAME=root

DB_PASSWORD=your_password



3.  Run Database Migration

Test the connection by running:

php artisan migrate






Step 3: Create Models, Migrations, and Controllers

1.  Create the Student Model and Migration

php artisan make:model Student -m




2.  Define the Student Table Schema

Open database/migrations/<timestamp>_create_students_table.php and define the schema:

public function up()

{

    Schema::create('students', function (Blueprint $table) {

        $table->id();

        $table->string('name');

        $table->string('email')->unique();

        $table->string('phone');

        $table->string('address');

        $table->timestamps();

    });

}



3.  Run the Migration

php artisan migrate



4.  Create a Resource Controller

Generate a resource controller for handling CRUD operations:

php artisan make:controller StudentController --resource




Step 4: Implement CRUD in the Controller

Open app/Http/Controllers/StudentController.php and implement methods:

1.  Index (Display Students):

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $students = Student::all();
        return view('students.index', compact('students'));
    }

2.  Create (Show Form):

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('students.create');
    }

3.  Store (Save New Student):

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:students',
            'phone' => 'required',
        ]);
   
        Student::create($request->all());
        return redirect()->route('students.index')->with('success', 'Student added successfully!');
    }

4.  Edit (Show Edit Form):

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Student $student)
{
    return view('students.edit', compact('student'));
}

5.  Update (Save Changes):

    /**
     * Update the specified resource in storage.
     */
     public function update(Request $request, Student $student)
     {
         $request->validate([
             'name' => 'required',
             'email' => 'required|email|unique:students,email,' . $student->id,
             'phone' => 'required',
         ]);
     
         $student->update($request->all());
         return redirect()->route('students.index')->with('success', 'Student updated successfully!');
     }

6.  Destroy (Delete Student):

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Student $student)
    {
        $student->delete();
        return redirect()->route('students.index')->with('success', 'Student deleted successfully!');
    }

Step 5: Define Routes

Update routes/web.php:

web.php

<?php

use App\Http\Controllers\StudentController;

Route::resource('students', StudentController::class);

Route::get('/', function () {
    return view('welcome');
});


Step 6: Create Views

1.  Index View (resources/views/students/index.blade.php):

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Student Management</title>
</head>
<body>
    <h1>Students</h1>
    <a href="{{ route('students.create') }}">Add New Student</a>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</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>
                    <a href="{{ route('students.edit', $student->id) }}">Edit</a>
                    <form action="{{ route('students.destroy', $student->id) }}" method="POST" style="display:inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</body>
</html>

2.  Create/Edit View (resources/views/students/create.blade.php and resources/views/students/edit.blade.php):

create.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add/Edit Student</title>
</head>
<body>
    <h1>{{ isset($student) ? 'Edit Student' : 'Add New Student' }}</h1>
    <form action="{{ isset($student) ? route('students.update', $student->id) : route('students.store') }}" method="POST">
        @csrf
        @if(isset($student))
            @method('PUT')
        @endif
        <label>Name:</label>
        <input type="text" name="name" value="{{ $student->name ?? '' }}" required>
        <label>Email:</label>
        <input type="email" name="email" value="{{ $student->email ?? '' }}" required>
        <label>Phone:</label>
        <input type="text" name="phone" value="{{ $student->phone ?? '' }}" required>
        <button type="submit">{{ isset($student) ? 'Update' : 'Save' }}</button>
    </form>
</body>
</html>


edit.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Student</title>
    <!-- Add Bootstrap CSS for styling (optional) -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <h2>Edit Student</h2>
       
        <!-- Display validation errors if any -->
        @if ($errors->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <!-- Form to edit student details -->
        <form action="{{ route('students.update', $student->id) }}" method="POST">
            @csrf
            @method('PUT')

            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" name="name" id="name" class="form-control" value="{{ $student->name }}" required>
            </div>

            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" name="email" id="email" class="form-control" value="{{ $student->email }}" required>
            </div>

            <div class="form-group">
                <label for="phone">Phone:</label>
                <input type="text" name="phone" id="phone" class="form-control" value="{{ $student->phone }}" required>
            </div>


            <button type="submit" class="btn btn-primary">Update Student</button>
            <a href="{{ route('students.index') }}" class="btn btn-secondary">Cancel</a>
        </form>
    </div>

    <!-- Add Bootstrap JS for interactivity (optional) -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>


Step 7: Add Styling

Use Bootstrap or any CSS framework to style the application. You can include Bootstrap via CDN in your layouts/app.blade.php.


Step 8: Test the Application

  • Visit http://localhost:8000/students to view the student list.
  • Add, edit, and delete students to ensure CRUD operations work correctly.

This step-by-step guide will help you build a basic Student Management System. You can extend this system with features like user authentication, advanced validation, or reporting as required.


 

Tags

Post a Comment

0Comments

Post a Comment (0)