This project is a simple RESTful API built with Laravel and SQLite, providing endpoints for managing users. It allows for creating, reading, updating, and deleting users, while utilizing Laravel's built-in functionality for validation, error handling, and database migrations.
- User management: CRUD (Create, Read, Update, Delete) functionality for users.
- Input validation: Validates data before it is saved or updated.
- Error handling: Handles and logs errors in a structured manner.
- Database migration: Auto-generates database schema with Laravel migrations.
- SQLite: In-memory or file-based database for lightweight and simple setup.
- PHP 8.x or higher
- Composer
- Laravel 10.x or higher
- SQLite or any other supported database (PostgreSQL, MySQL, etc.)
- cURL (for testing API endpoints)
Follow the steps below to get the project running:
git clone https://github.com/yourusername/user-management-api.git
cd user-management-api
Run the following command to install Laravel dependencies:
composer install
Copy the example environment file and update the necessary variables:
cp .env.example .env
Make sure to configure the database settings in .env
to use SQLite:
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database/database.sqlite
Create the SQLite database file:
touch database/database.sqlite
php artisan key:generate
To create the necessary tables in the database, run:
php artisan migrate
You can run the application locally using Laravel’s development server:
php artisan serve
The application will now be running at http://127.0.0.1:8000
.
Here are the main API endpoints for managing users:
Create a new user.
-
Request Body (JSON):
{ "name": "John Doe", "email": "[email protected]", "password": "secret", "role": "user" }
-
Response:
201 Created
: User created successfully.
Retrieve a list of all users.
- Response:
200 OK
: List of users.
Retrieve a specific user by ID.
- Response:
200 OK
: User found.404 Not Found
: User not found.
Update a specific user by ID.
-
Request Body (JSON):
{ "name": "John Updated", "email": "[email protected]", "password": "newpassword", "role": "admin" }
-
Response:
200 OK
: User updated successfully.404 Not Found
: User not found.
Delete a specific user by ID.
- Response:
204 No Content
: User deleted successfully.404 Not Found
: User not found.
You can test the API endpoints using cURL:
curl -X POST http://127.0.0.1:8000/api/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "[email protected]", "password": "secret", "role": "user"}'
curl -X GET http://127.0.0.1:8000/api/users
curl -X GET http://127.0.0.1:8000/api/users/1
curl -X PUT http://127.0.0.1:8000/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "John Updated", "email": "[email protected]", "password": "newpassword", "role": "admin"}'
curl -X DELETE http://127.0.0.1:8000/api/users/1