1.
Initialize the Laravel Project
First, create the Laravel project and configure the environment:
bash
Copy code
composer create-project --prefer-dist laravel/laravel ChallanTrackingSystem
cd ChallanTrackingSystem
2. Set Up Database
In the .env file, configure your database:
plaintext
Copy code
DB_CONNECTION=mysql
DB_HOST=[Link]
DB_PORT=3306
DB_DATABASE=challan_tracking
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password
Run migration to set up default tables for users:
bash
Copy code
php artisan migrate
3. Install Laravel Breeze for Authentication
Laravel Breeze offers a simple way to set up authentication.
bash
Copy code
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
4. Create User Roles and Middleware
Add roles to users by creating a migration for the role column in
the users table.
bash
Copy code
php artisan make:migration add_role_to_users_table --table=users
In the migration file, add a role column:
php
Copy code
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('role')->default('user');
});
}
Run the migration:
bash
Copy code
php artisan migrate
In [Link], define constants for roles:
php
Copy code
public const ADMIN = 'admin';
public const CHALLAN_USER = 'challan_user';
5. Define User Access Middleware
Create middleware for role-based access control:
bash
Copy code
php artisan make:middleware RoleMiddleware
In the RoleMiddleware file:
php
Copy code
public function handle($request, Closure $next, ...$roles)
{
if (!in_array($request->user()->role, $roles)) {
abort(403, 'Unauthorized');
}
return $next($request);
}
Register the middleware in [Link]:
php
Copy code
protected $routeMiddleware = [
'role' => \App\Http\Middleware\RoleMiddleware::class,
];
6. Create Models and Migrations for Challans
Define the Challan model and migration:
bash
Copy code
php artisan make:model Challan -m
In the migration file for challans:
php
Copy code
public function up()
{
Schema::create('challans', function (Blueprint $table) {
$table->id();
$table->string('FIR_no');
$table->string('offence');
$table->date('date_of_creation');
$table->string('title');
$table->enum('type', ['incomplete', 'complete']);
$table->string('police_station');
$table->text('remarks')->nullable();
$table->timestamps();
});
}
Run the migration:
bash
Copy code
php artisan migrate
7. Set Up Controllers
Create controllers for the admin dashboard and challan
management.
bash
Copy code
php artisan make:controller AdminController
php artisan make:controller ChallanController
In ChallanController, add methods for adding a challan:
php
Copy code
public function create()
{
return view('[Link]');
}
public function store(Request $request)
{
$request->validate([
'FIR_no' => 'required',
'offence' => 'required',
'date_of_creation' => 'required|date',
'title' => 'required',
'type' => 'required|in:incomplete,complete',
'police_station' => 'required',
'remarks' => 'nullable|string',
]);
Challan::create($request->all());
return redirect()->route('[Link]');
}
8. Define Routes
In [Link], set up routes for the different user roles.
php
Copy code
// Admin routes
Route::middleware(['auth', 'role:admin'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'index'])-
>name('[Link]');
});
// Challan user routes
Route::middleware(['auth', 'role:challan_user'])->group(function () {
Route::resource('challans', ChallanController::class);
});
9. Create Views
for the Challan form (FIR No, Offence,
challans/[Link]
Date of creation, etc.).
admin/[Link] for the Admin dashboard to display
entries from other users.
10. Test the Application
Start the Laravel development server and test the app:
bash
Copy code
php artisan serve