A modern Instagram-like backend application built with Laravel 12, providing RESTful APIs for social media functionality including posts, comments, and likes.
- Tech Stack
- Features
- Database Structure
- API Endpoints
- Installation
- Configuration
- Running the Application
- Framework: Laravel 12.x
- PHP: ^8.2
- Database: PostgreSQL
- Authentication: Laravel Sanctum
- File Storage: Cloudflare R2 (S3-compatible via Flysystem)
- ID Generation: ULID (26-character sortable IDs)
- 🔐 Authentication (Register, Login, Logout)
- 👤 User Profile Management
- 📸 Post Creation & Management (with image upload)
- 💬 Comments on Posts
- ❤️ Like/Unlike Posts
- 🔒 Secure API with Sanctum Token Authentication
- 📦 Optimized Database Queries with Relationships
- 🎨 RESTful API Design
The application uses a relational database with the following tables:
| Column | Type | Constraints |
|---|---|---|
| id | char(26) | PRIMARY KEY, ULID |
| name | varchar(255) | NOT NULL |
| varchar(100) | UNIQUE, NOT NULL | |
| password | varchar(255) | NOT NULL |
| created_at | timestamp | NOT NULL |
| updated_at | timestamp | NOT NULL |
| Column | Type | Constraints |
|---|---|---|
| id | char(26) | PRIMARY KEY, ULID |
| caption | text | NULLABLE |
| image_path | text | NOT NULL |
| user_id | char(26) | FOREIGN KEY → users.id |
| created_at | timestamp | NOT NULL |
| updated_at | timestamp | NOT NULL |
| Column | Type | Constraints |
|---|---|---|
| id | char(26) | PRIMARY KEY, ULID |
| text | varchar(500) | NOT NULL |
| post_id | char(26) | FOREIGN KEY → posts.id |
| user_id | char(26) | FOREIGN KEY → users.id |
| created_at | timestamp | NOT NULL |
| updated_at | timestamp | NOT NULL |
| Column | Type | Constraints |
|---|---|---|
| id | char(26) | PRIMARY KEY, ULID |
| user_id | char(26) | FOREIGN KEY → users.id |
| post_id | char(26) | FOREIGN KEY → posts.id |
| created_at | timestamp | NOT NULL |
| updated_at | timestamp | NOT NULL |
users (1) ──────< (many) posts
users (1) ──────< (many) comments
users (1) ──────< (many) likes
posts (1) ──────< (many) comments
posts (1) ──────< (many) likes
All foreign key relationships have ON DELETE CASCADE to maintain referential integrity.
All API endpoints are prefixed with /api/v1.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /auth/register |
Register new user | ❌ |
| POST | /auth/login |
Login user | ❌ |
| GET | /auth/me |
Get authenticated user profile | ✅ |
| POST | /auth/logout |
Logout user | ✅ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /posts |
Get all posts (paginated) | ✅ |
| GET | /posts/me |
Get authenticated user's posts | ✅ |
| GET | /posts/{id} |
Get single post | ✅ |
| POST | /posts |
Create new post | ✅ |
| PUT | /posts/{id} |
Update post | ✅ |
| DELETE | /posts/{id} |
Delete post | ✅ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /posts/{id}/comments |
Get all comments for a post | ✅ |
| POST | /posts/{id}/comments |
Create comment on a post | ✅ |
| PUT | /comments/{id} |
Update comment | ✅ |
| DELETE | /comments/{id} |
Delete comment | ✅ |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /posts/{id}/like |
Toggle like on a post | ✅ |
- PHP 8.2 or higher
- Composer
- PostgreSQL
- Node.js & NPM (for frontend assets)
-
Clone the repository
git clone <repository-url> cd InstaApp-be
-
Install dependencies
composer install npm install
-
Environment configuration
cp .env.example .env
-
Generate application key
php artisan key:generate
-
Configure database
Update your
.envfile with database credentials:DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=instaapp_be DB_USERNAME=your_username DB_PASSWORD=your_password
-
Configure Cloudflare R2 (for image uploads)
Update your
.envfile with Cloudflare R2 credentials:CLOUDFLARE_ACCESS_KEY_ID=your_r2_access_key_id CLOUDFLARE_SECRET_ACCESS_KEY=your_r2_secret_access_key CLOUDFLARE_REGION=auto CLOUDFLARE_BUCKET=your_r2_bucket_name CLOUDFLARE_ENDPOINT=https://<account_id>.r2.cloudflarestorage.com CLOUDFLARE_URL=your_public_r2_url
Note: Cloudflare R2 is S3-compatible and uses Flysystem for storage operations.
-
Run migrations
php artisan migrate
-
Build frontend assets
npm run build
Run the setup script to install all dependencies and configure the application:
composer setupThis command will:
- Install PHP dependencies
- Create
.envfile from example - Generate application key
- Run database migrations
- Install NPM packages
- Build frontend assets
Run the development server with hot-reload and queue worker:
composer devThis command runs three processes concurrently:
- Laravel development server (port 8000)
- Queue worker
- Vite development server
Alternatively, start services individually:
# Start Laravel server
php artisan serve
# Start queue worker (in another terminal)
php artisan queue:listen
# Start Vite dev server (in another terminal)
npm run devThe API will be available at http://localhost:8000.
This project is open-sourced software licensed under the MIT license.