Laravel Mini CRM for Beginners in 1 Hour: Step-by-Step
Table of Contents
Introduction
In this tutorial, you'll learn how to build a simple Customer Relationship Management (CRM) application using Laravel within an hour. This step-by-step guide is designed for beginners and covers the essential features including user management, roles, and CRUD operations for clients, projects, and tasks. By the end of this tutorial, you’ll have a functional mini CRM that you can expand upon.
Step 1: Install Laravel and Breeze
To start, ensure you have Composer installed on your machine. Follow these steps to set up Laravel and Breeze.
-
Create a new Laravel project:
composer create-project --prefer-dist laravel/laravel mini-crm
-
Navigate to the project directory:
cd mini-crm
-
Install Laravel Breeze for authentication:
composer require laravel/breeze --dev
-
Run Breeze installation:
php artisan breeze:install
-
Migrate the database:
php artisan migrate
-
Install frontend dependencies and compile assets:
npm install && npm run dev
-
Start the Laravel development server:
php artisan serve
Step 2: User CRUD Operations
Next, implement CRUD operations for user management.
-
Create a UserController:
php artisan make:controller UserController
-
Define routes in
web.php
:Route::resource('users', UserController::class);
-
Implement CRUD methods in
UserController
:- Index: List all users.
- Create: Show form for creating a new user.
- Store: Save the new user.
- Edit: Show form for editing an existing user.
- Update: Save the updated user.
- Destroy: Delete a user.
-
Create views for each of the CRUD operations in
resources/views/users
.
Step 3: Adding Admin Role
To enhance user management, add an admin role.
-
Add role column to the users table:
php artisan make:migration add_role_to_users_table --table=users
-
Update the migration file to include the role column:
$table->string('role')->default('user');
-
Run the migration:
php artisan migrate
-
Update the User registration and editing forms to include a role selection.
Step 4: Clients CRUD Operations
Now, let's create a CRUD interface for managing clients.
-
Create a Client model and migration:
php artisan make:model Client -m
-
Define the client fields in the migration file and run:
php artisan migrate
-
Create ClientController for CRUD operations:
php artisan make:controller ClientController
-
Define routes for clients similar to users.
-
Implement CRUD methods in
ClientController
and create corresponding views.
Step 5: Projects CRUD Operations
Next, implement CRUD operations for projects.
-
Create a Project model and migration:
php artisan make:model Project -m
-
Define project fields in the migration file and run:
php artisan migrate
-
Create ProjectController and set up routes.
-
Implement CRUD methods in
ProjectController
and create views.
Step 6: Tasks CRUD Operations
Finally, add CRUD functionalities for tasks.
-
Create a Task model and migration:
php artisan make:model Task -m
-
Define task fields in the migration file and run:
php artisan migrate
-
Create TaskController and define routes.
-
Implement CRUD methods in
TaskController
and create views.
Step 7: Permissions for Delete
To manage permissions effectively, especially for delete actions:
-
Use Laravel policies to handle user permissions.
-
Create a policy for users:
php artisan make:policy UserPolicy
-
Define permissions within the policy methods.
-
Register the policy in
AuthServiceProvider
. -
Use the policy in your controllers to restrict delete actions based on user roles.
Conclusion
You have now built a mini CRM application using Laravel. You have implemented user authentication, CRUD operations for users, clients, projects, and tasks, and added role management and permissions.
Next Steps
- Explore Laravel's advanced features like middleware and event broadcasting.
- Customize the UI using frontend frameworks or libraries.
- Consider deploying your application to a hosting platform.