ARTICLE AD BOX
I am building an Employee Management System using Laravel Breeze for authentication.
CONTROLLER
<?php namespace App\Http\Controllers; use App\Models\Employee; use Illuminate\Http\Request; use Barryvdh\DomPDF\Facade\Pdf; //PDF Library class EmployeeController extends Controller { /** * Display a listing of the resource. */ //SHOW ALL EMPLOYEES public function index() { //Get all employees from database $employees = Employee::all(); //Send data to blade view return view ('employees.index', compact('employees')); } /** * Show the form for creating a new resource. */ //SHOW CREATE EMPLOYEE FORM public function create() { return view('employees.create'); } /** * Store a newly created resource in storage. */ //STORE EMPLOYEE IN DATABASE public function store(Request $request) { Employee::create([ 'name'=>$request->name, 'position' => $request->position, 'department' =>$request->department, 'salary'=>$request->salary, ]); return redirect()->route('employees.index')->with('success', 'Employee added successfully'); } /** * Display the specified resource. */ public function show(Employee $employee) { // } /** * Show the form for editing the specified resource. */ public function edit(Employee $employee) { return view('employees.edit', compact('employee')); } /** * Update the specified resource in storage. */ //UPDATE EMPLOYEE public function update(Request $request, Employee $employee) { $request->validate([ 'name' => 'required', 'position' => 'required', 'department' => 'required', 'salary' => 'required|integer', ]); //Update Record $employee->update($request->all()); return redirect()->route('employees.index')-> with('success', 'Employee updated successfully'); } /** * Remove the specified resource from storage. */ //DELETE EMPLOYEE public function destroy(Employee $employee) { $employee->delete(); return redirect()->route('employees.index')->with('success', 'Employee deleted successfully'); } //EXPORT TO PDF public function exportPDF() { //GET ALL EMPLOYEES $employees = Employee::all(); //LOAD BLADE VIEW INTO PDF $pdf = Pdf::loadView('employees.pdf', compact('employees')); //DOWNLOAD FILE return $pdf->download('employees.pdf'); } }MODEL
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Employee extends Model { //Allow mass assignment (important for create/update) protected $fillable = [ 'name', 'position', 'department', 'salary' ]; }Table Migration
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('employees', function (Blueprint $table) { $table->id(); //Employee name $table->string('name'); //Position $table->string ('position'); //Department $table->string('department'); //Salary $table->integer('salary'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('employees'); } };USERSEEDER
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Hash; use App\Models\User; class UserSeeder extends Seeder { public function run(): void { User::create([ 'name' => 'Admin User', 'email' => '[email protected]', 'password' => Hash::make('password123'), ]); User::create([ 'name' => 'John Doe', 'email' => '[email protected]', 'password' => Hash::make('password123'), ]); } }DATABASESEEDER
<?php namespace Database\Seeders; use App\Models\User; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { use WithoutModelEvents; /** * Seed the application's database. */ public function run(): void { $this->call([ UserSeeder::class, ]); } }CREATE BLADE
<style>
body {
font-family: Arial, sans-serif;
background: #f2f4f8;
margin: 0;
padding: 0;
}
.container {
width: 400px;
margin: 80px auto;
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 12px;
border: 1px solid #ccc;
border-radius: 6px;
outline: none;
transition: 0.3s;
}
input:focus {
border-color: #4a90e2;
box-shadow: 0 0 5px rgba(74,144,226,0.3);
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
transition: 0.3s;
}
button:hover {
background: #218838;
}
.container:hover {
transform: translateY(-2px);
transition: 0.3s;
}
</style>
</head>
<body>
<div class="container">
<h2>Add Employee</h2>
<form action="{{ route('employees.store') }}" method="POST">
@csrf
<input type="text" name="name" placeholder="Name">
<input type="text" name="position" placeholder="Position">
<input type="text" name="department" placeholder="Department">
<input type="text" name="salary" placeholder="Salary">
<button type="submit">Save</button>
</form>
</div>
</body>
</html>
EDIT BLADE
<!DOCTYPE html> <html> <head> <title>Edit Employee</title> </head> <body> <div class ="container"> <h2>Edit Employee</h2> <form action="{{route('employees.update', $employee->id)}}" method ="POST"> @csrf @method('PUT') <input type="text" name = "name" value="{{$employee->name}}"><br><br> <input type="text" name ="position" value="{{$employee->position}}"><br><br> <input type="text" name ="department" value="{{$employee->department}}"><br><br> <input type="number" name ="salary" value="{{$employee->salary}}"><br><br> <button type = "submit">Update</button> </form> </div> </body> </html>INDEX
<!-- EMPLOYEE LIST PAGE--> <!DOCTYPE html> <html> <head> <title>Employees</title> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container mt-5"> <h2>Employee Management System</h2> <!--BUTTON TO CREATE EMPLOYEE--> <a href="{{route('employees.create')}}" class= "btn btn-primary"> Add Employee </a> <!--BUTTON TO EXPORT PDF--> <a href ="{{route('employees.pdf')}}" class ="btn btn-danger"> Export PDF </a> <!--LOGOUT BUTTON--> <form action="{{route('logout')}}" method = "POST" style="display:inline;"> @csrf <button type = "submit" class = "btn btn-secondary"> Logout </button> </form> <br><br> <!--SUCCESS MESSAGE--> @if(session('success')) <div class ="alert alert-success"> {{session('success')}} </div> @endif <!--EMPLOYEE TABLE--> <table class = "table table-bordered"> <tr> <th>Name</th> <th>Postion</th> <th>Department</th> <th>Salary</th> <th>Action</th> </tr> @foreach($employees as $employee) <tr> <td>{{ $employee->name}}</td> <td>{{$employee->position}}</td> <td>{{$employee->department}}</td> <td>{{$employee->salary}}</td> <td> <!--EDIT--> <a href = "{{route('employees.edit', $employee->id)}}" class="btn btn-warning">Edit</a> <!--DELETE--> <form action="{{route ('employees.destroy', $employee->id)}}" method="POST" style="display:inline;"> @csrf @method('DELETE') <button class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> </div> </body> </html>PDF EXPORT
<h2>Employee List</h2> <table border ="1" width="100%"> <tr> <th>Name</th> <th>Position</th> <th>Department</th> <th>Salary</th> </tr> @foreach($employees as $employee) <tr> <td>{{$employee->name}}</td> <td>{{$employee->position}}</td> <td>{{$employee->department}}</td> <td>{{$employee->salary}}</td> </tr> @endforeach </table>WEB
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\EmployeeController; /* |-------------------------------------------------------------------------- | HOME ROUTE |-------------------------------------------------------------------------- */ Route::get('/', function () { return redirect()->route('employees.index'); }); /* |-------------------------------------------------------------------------- | AUTH ROUTES |-------------------------------------------------------------------------- */ require __DIR__.'/auth.php'; /* |-------------------------------------------------------------------------- | PROTECTED ROUTES |-------------------------------------------------------------------------- */ Route::middleware(['auth'])->group(function () { // EMPLOYEES CRUD Route::resource('employees', EmployeeController::class); // PDF EXPORT Route::get('/export-pdf', [EmployeeController::class, 'exportPDF']) ->name('employees.pdf'); });SAMPLE SAMPLE BEST PRACTICE
I am building an Employee Management System using Laravel Breeze for authentication.
I am building an Employee Management System using Laravel Breeze for authentication.
