- Using Postman or cURL: We can test our API using tools like Postman or cURL. Here are some example requests:
- GET All Tasks: This retrieves a list of all tasks.
- POST Create Task: This creates a new task by sending a JSON payload.
- GET Task by ID: This fetches a specific task.
- PUT Update Task: This updates the task’s completion status.
- DELETE Task: This deletes a task by ID.
Category: Examples
https://cdn-icons-png.flaticon.com/512/5307/5307812.png
-
Testing the API
-
Creating a Controller
- Controller Creation: We generate a resource controller using Artisan. Resource controllers simplify the creation of CRUD (Create, Read, Update, Delete) operations by providing a standard set of methods.
- Defining Methods: In
TaskController, we implement several methods:index(): Returns all tasks from the database.store(Request $request): Accepts a request with task data, creates a new task, and returns it with a 201 status.show($id): Retrieves a specific task by its ID.update(Request $request, $id): Updates a task based on incoming data and returns the updated task.destroy($id): Deletes a task and responds with a 204 status (no content).
-
Defining Routes
- API Routes: We define routes in
routes/api.phpusing theapiResourcemethod. This method automatically sets up all the necessary routes for our RESTful API:- GET
/api/tasksfor retrieving tasks. - POST
/api/tasksfor creating a new task. - GET
/api/tasks/{id}for retrieving a specific task. - PUT
/api/tasks/{id}for updating a task. - DELETE
/api/tasks/{id}for deleting a task.
- GET
- API Routes: We define routes in
-
Creating a Migration for Tasks
- Migration Creation: We use Artisan to create a migration file for the
taskstable. Migrations allow us to define the structure of the database in PHP code, making it easy to version control database changes. - Schema Definition: In the migration file, we define the schema for the
taskstable, which includes:id: An auto-incrementing primary key.title: A string to store the task title.description: A nullable text field for additional details.completed: A boolean to track if the task is finished (defaulting tofalse).timestamps: Automatically managed fields for created and updated times.
- Running the Migration: By running
php artisan migrate, we apply this migration, creating thetaskstable in the database.
- Migration Creation: We use Artisan to create a migration file for the
-
Creating a Model
- Task Model: The model acts as an interface between the application and the database. By creating the
Taskmodel, we enable Eloquent ORM functionalities, which allow for easy database interactions.
- Task Model: The model acts as an interface between the application and the database. By creating the
-
Setting Up Laravel
- Installation: We start by using Composer to create a new Laravel project called
task-manager. This command sets up the basic structure of a Laravel application, including necessary directories and configuration files.
- Installation: We start by using Composer to create a new Laravel project called