A comprehensive Laravel 11 REST API for creating and managing polls with voting functionality.
- ✅ JWT-based authentication using Laravel Sanctum
- ✅ User registration, login, logout
- ✅ User profile management
- ✅ Token-based API access
- ✅ Create, read, update, delete polls
- ✅ Poll ownership and authorization
- ✅ Poll scheduling (start/end times)
- ✅ Poll status management (active/inactive)
- ✅ Poll search and filtering
- ✅ Multiple options per poll
- ✅ Option ordering
- ✅ Dynamic option management
- ✅ One vote per user per poll (configurable)
- ✅ Anonymous voting support
- ✅ Multiple votes per user (configurable)
- ✅ Vote tracking and management
- ✅ Prevent duplicate voting
- ✅ Real-time vote counting
- ✅ Vote percentages calculation
- ✅ Poll results endpoint
- ✅ Vote history tracking
- ✅ RESTful API design
- ✅ Resource-based responses
- ✅ Comprehensive validation
- ✅ Error handling with standardized format
- ✅ Pagination support
- ✅ Soft deletes
-
Clone the repository
git clone <repository-url> cd poll-voting-api
-
Install dependencies
composer install
-
Environment setup
cp .env.example .env php artisan key:generate
-
Database setup
# Configure your database in .env file php artisan migrate php artisan db:seed
-
Start the development server
php artisan serve
POST /api/auth/register
- Register new userPOST /api/auth/login
- Login userPOST /api/auth/logout
- Logout userGET /api/auth/user
- Get authenticated user
GET /api/polls
- Get all polls (with filters)GET /api/polls/{id}
- Get single pollPOST /api/polls
- Create new pollPUT /api/polls/{id}
- Update pollDELETE /api/polls/{id}
- Delete pollGET /api/polls/{id}/results
- Get poll results
POST /api/votes/polls/{id}
- Cast voteGET /api/votes
- Get user's votesDELETE /api/votes/{id}
- Delete vote
GET /api/health
- API health status
curl -X POST http://localhost:8000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "[email protected]",
"password": "password123",
"password_confirmation": "password123"
}'
curl -X POST http://localhost:8000/api/polls \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"title": "What is your favorite programming language?",
"description": "Choose your preferred programming language",
"start_time": "2024-01-01T10:00:00Z",
"end_time": "2024-01-07T23:59:59Z",
"allow_anonymous_voting": true,
"allow_multiple_votes": false,
"options": ["JavaScript", "Python", "PHP", "Java"]
}'
curl -X POST http://localhost:8000/api/votes/polls/1 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"poll_option_id": 1
}'
curl -X GET http://localhost:8000/api/polls/1/results \
-H "Accept: application/json"
?active=true
- Get only active polls?my_polls=true
- Get user's own polls?search=keyword
- Search polls by title?page=1
- Pagination
All API responses follow this standardized format:
{
"status": "success|error",
"message": "Response message",
"data": {
// Response data
},
"errors": {
// Validation errors (if any)
}
}
The API uses Laravel Policies for authorization:
- Only poll creators can update/delete their polls
- Users can only delete their own votes
- Anonymous voting is supported when enabled
id
,name
,email
,password
,email_verified_at
,remember_token
,created_at
,updated_at
id
,title
,description
,user_id
,start_time
,end_time
,allow_anonymous_voting
,allow_multiple_votes
,is_active
,created_at
,updated_at
,deleted_at
id
,poll_id
,text
,order
,created_at
,updated_at
,deleted_at
id
,poll_id
,poll_option_id
,user_id
,ip_address
,user_agent
,created_at
,updated_at
,deleted_at
The API includes a comprehensive Postman collection (postman_collection.json
) with all endpoints and example requests.
- ✅ User authentication and authorization
- ✅ Poll CRUD operations
- ✅ Poll options management
- ✅ Voting system with duplicate prevention
- ✅ Anonymous voting support
- ✅ Real-time results and analytics
- ✅ RESTful API design
- ✅ Comprehensive validation
- ✅ Error handling
- ✅ Multiple votes per user (configurable)
- ✅ Soft deletes for all entities
- ✅ Poll scheduling and auto-deactivation
- ✅ Search and filtering
- ✅ Pagination
- ✅ IP tracking for anonymous votes
- ✅ Policy-based authorization
- ✅ Token-based authentication
- ✅ Input validation and sanitization
- ✅ Authorization policies
- ✅ Rate limiting ready
- ✅ CORS configuration
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is open-sourced software licensed under the MIT license.