How to Build a Simple Laravel Task Manager using Laravel 10.8.0 and DomainIndia.com cPanel Hosting Print

  • 0

Title: How to Build a Simple Laravel Task Manager using Laravel 10.8.0 and DomainIndia.com cPanel Hosting

Introduction:
A task manager is a straightforward web application that allows users to manage their tasks, including creating, editing, and deleting tasks, as well as marking them as complete. In this article, we'll walk you through the process of building a simple task manager using Laravel 10.8.0 and DomainIndia.com cPanel hosting.

Step 1: Set up your Laravel project on DomainIndia.com cPanel hosting
Follow the steps outlined in the previous article to install Laravel 10.8.0 on your DomainIndia.com cPanel hosting account.

Step 2: Create a new database and configure the .env file
Create a new database in your DomainIndia.com cPanel account and note down the database name, username, and password. Then, update the ".env" file in your Laravel project with the correct database credentials:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Step 3: Use Laravel's Eloquent ORM to create a Task model and migration
In your Laravel project, run the following command to create a Task model and a corresponding migration file:


php artisan make:model Task -m

Open the newly created migration file located in the "database/migrations" directory and update the "up" method with the following code to define the tasks table schema:


public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->text('description')->nullable();
$table->boolean('is_completed')->default(false);
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

After updating the migration file, run the following command to apply the migration and create the tasks table in your database:


php artisan migrate

Step 4: Create a TaskController to handle CRUD operations
To create a TaskController, run the following command in your Laravel project:


php artisan make:controller TaskController --resource

This command will generate a resource controller with methods for handling CRUD operations. Open the "TaskController.php" file located in the "app/Http/Controllers" directory and add the necessary code for each method.

Step 5: Add routes to the "routes/web.php" file
Open the "routes/web.php" file and add the following code to define routes for the TaskController:


Route::middleware(['auth'])->group(function () {
Route::resource('tasks', TaskController::class);
});

This code will create routes for all CRUD operations in the TaskController, protected by Laravel's built-in authentication middleware.

Step 6: Implement views using the Blade templating engine
Create new Blade templates in the "resources/views" directory to display tasks, create new tasks, and edit existing tasks. Use Laravel's built-in components, such as forms, buttons, and validation error messages, to create a user-friendly interface.

Step 7: Use Laravel's built-in authentication
To restrict access to the task manager application, use Laravel's built-in authentication system. Run the following command to scaffold basic authentication views and routes:


composer require laravel/ui
php artisan ui bootstrap --auth
npm install
npm run dev

After running these commands, your task manager application will have registration and login functionality

Step 8: Modify the TaskController
Now that the authentication system is in place, modify the TaskController to associate tasks with the authenticated user. Update the "index" method to retrieve only the tasks for the logged-in user:


public function index()
{
$tasks = auth()->user()->tasks;
return view('tasks.index', compact('tasks'));
}

Similarly, update the "store" and "update" methods to save tasks with the authenticated user's ID:


public function store(Request $request)
{
$request->validate([
'title' => 'required|max:255',
'description' => 'nullable',
]);

$task = new Task($request->all());
$task->user_id = auth()->user()->id;
$task->save();

return redirect()->route('tasks.index')->with('success', 'Task created successfully.');
}

public function update(Request $request, Task $task)
{
$request->validate([
'title' => 'required|max:255',
'description' => 'nullable',
]);

$task->update($request->all());
return redirect()->route('tasks.index')->with('success', 'Task updated successfully.');
}

Step 9: Customize views with user information
In your views, you can now display information specific to the authenticated user. For example, you can display the user's name in the task list view by adding the following code to the "resources/views/tasks/index.blade.php" file:


<h3>Welcome, {{ auth()->user()->name }}!</h3>


Step 10: Test your Task Manager application
With all components in place, it's time to test your Laravel Task Manager application. Access your application in a web browser, register a new user, log in, and start managing tasks. You should be able to create, edit, and delete tasks, as well as mark them as complete.

Conclusion:
In this article, we've shown you how to build a simple task manager application using Laravel 10.8.0 and DomainIndia.com cPanel hosting. By following these steps, you can create a functional and user-friendly task management system with built-in authentication. This project can serve as a foundation for more complex applications or as a starting point for learning more about Laravel development.


Was this answer helpful?

« Back