A complete REST API for managing a small library: authors, categories, books, borrowings, and users.
This project was built as a Laravel exercise to master modern ecosystem concepts (Eloquent, API Resources, Sanctum, Services, Events, Policies, Tests, etc.).
- Full CRUD for authors, books, and categories
- Borrowing management (borrow / return a book)
- Advanced filtering on books (available, by author, by category, published)
- Token-based authentication with Laravel Sanctum (register, login, logout)
- Custom validation + standardized error responses
- Custom cast for prices (cents → dollars)
- Enum for book status
- Eloquent query scopes + many-to-many relationships
- Service layer (BorrowingService) + database transactions
- Trait for logging model create/update/delete events
- Broadcastable event when a book is borrowed
- Policy to allow only the borrower to return their book
- Custom pagination format
- Artisan command
library:stats - Feature & unit tests
- PHP 8.1+
- Laravel 10.x
- Laravel Sanctum (token authentication)
- MySQL / MariaDB (or SQLite for testing)
- Eloquent ORM
- API Resources + Form Requests
- PHPUnit + Laravel testing suite
- Clone the project
git clone https://github.com/Tedel12/library-api.git
cd library-api- Install PHP dependencies
composer install- Copy the environment file
cp .env.example .env- Generate application key
php artisan key:generate-
Configure your database in
.env- Set
DB_CONNECTION=mysql(orsqlitefor quick testing) - Fill in
DB_HOST,DB_PORT,DB_DATABASE,DB_USERNAME,DB_PASSWORD
- Set
-
Run migrations (create tables)
php artisan migrate- Start the local development server
php artisan serveThe API will be available at:
http://127.0.0.1:8000/api/v1
You can now test endpoints with tools like Postman, Insomnia or curl.
- Register a user:
POST /api/v1/auth/register - Login:
POST /api/v1/auth/login - Get books:
GET /api/v1/books(with Bearer token in Authorization header)