0% found this document useful (0 votes)
2 views

laravel chatgpt

This document outlines the steps to create a Challan Tracking System using Laravel. It includes initializing the project, setting up the database, installing authentication, creating user roles and middleware, defining models and migrations for challans, setting up controllers and routes, creating views, and testing the application. Each step provides specific commands and code snippets to guide the user through the process.

Uploaded by

itpersonals2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

laravel chatgpt

This document outlines the steps to create a Challan Tracking System using Laravel. It includes initializing the project, setting up the database, installing authentication, creating user roles and middleware, defining models and migrations for challans, setting up controllers and routes, creating views, and testing the application. Each step provides specific commands and code snippets to guide the user through the process.

Uploaded by

itpersonals2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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=127.0.0.1
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 User.php, 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 Kernel.php:


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('challans.create');
}

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('challans.index');
}

8. Define Routes

In web.php, 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('admin.dashboard');
});

// 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/create.blade.php
Date of creation, etc.).
 admin/dashboard.blade.php 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

You might also like