A Laravel-based RESTful API for managing blog posts and categories.
- Authentication with Laravel Sanctum
- Post management (CRUD operations)
- Category management
- Role-based access control (Admin/User)
- Soft deletes for posts
- Media uploads for post images
- Clone the repository:
git clone https://github.com/developermithu/api-blog-laravel.git
- Change directory:
cd api-blog-laravel
- Install dependencies:
composer install
- Copy the
.env.example
file to.env
:
cp .env.example .env
- Generate the application key:
php artisan key:generate
- Update the database configuration in the
.env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=api-blog-laravel
DB_USERNAME=root
DB_PASSWORD=
- Migrate and seed the database:
php artisan migrate:fresh --seed
- Start the development server:
php artisan serve
- Open the application in your web browser at
http://localhost:8000
.
All authentication endpoints are prefixed with /api/auth
.
POST /api/auth/register
Parameter | Type | Description |
---|---|---|
name |
string |
Required. User's name |
email |
string |
Required. User's email |
password |
string |
Required. User's password |
POST /api/auth/login
Parameter | Type | Description |
---|---|---|
email |
string |
Required. User's email |
password |
string |
Required. User's password |
Successful login returns a Bearer token that should be used for authenticated requests.
POST /api/auth/logout
Requires authentication. Invalidates the current access token.
GET /api/posts
Supports filtering by:
GET /api/posts?search=query&status=draft&is_featured=true&filter=trash&page=1&per_page=6
- search query [search=query]
- status [status=draft/published]
- featured posts [is_featured=true/false]
- trashed posts [filter=all/trash/with_trashed]
GET /api/posts/{slug}
POST /api/posts
Parameter | Type | Description |
---|---|---|
title |
string |
Required. Post title |
slug |
string |
Required. Post slug |
excerpt |
string |
Required. Post excerpt |
content |
string |
Required. Post content |
category_id |
integer |
Required. Category ID |
status |
string |
Required. Post status (draft/published) |
is_featured |
boolean |
Optional. Featured post status |
cover_image |
file |
Optional. Post image |
PUT /api/posts/{slug}
Accepts the same parameters as the create endpoint.
DELETE /api/posts/{slug}
Soft deletes the post. The post can be restored later.
POST /api/posts/{id}/restore
Restores a soft-deleted post.
DELETE /api/posts/{id}/force-delete
Permanently deletes the post.
GET /api/categories
GET /api/categories/{category}
POST /api/categories
Parameter | Type | Description |
---|---|---|
name |
string |
Required. Category name |
slug |
string |
Required. Category slug |
PUT /api/categories/{category}
DELETE /api/categories/{category}
All admin-only endpoints require authentication using a Bearer token. Include the token in the Authorization header:
Authorization: Bearer <your_token>
The API uses standard HTTP status codes to indicate the success or failure of requests:
200 OK
- Request succeeded201 Created
- Resource created successfully400 Bad Request
- Invalid request parameters401 Unauthorized
- Missing or invalid authentication token403 Forbidden
- Authenticated but not authorized to access the resource404 Not Found
- Resource not found422 Unprocessable Entity
- Validation errors
Made with ❤️ by developermithu