User model
The User model represents registered users who can create and manage blog posts.app/User.php
User attributes
Fillable attributes
Fillable attributes
These attributes can be mass-assigned when creating or updating users:
- name - User’s first name (required, alpha characters only)
- surname - User’s last name (required, alpha characters only)
- description - User’s bio or description (optional)
- email - User’s email address (required, must be unique)
- password - User’s hashed password (required, SHA-256)
User relationships
A user can have many posts:Post model
The Post model represents blog articles created by users.app/Post.php
Post attributes
Fillable attributes
Fillable attributes
These attributes can be mass-assigned:
- title - Post title (required)
- content - Post body/content (required)
- category_id - Foreign key to categories table (required)
- image - Filename of the post’s featured image (required)
- user_id - Foreign key to users table (set automatically from JWT token)
Post relationships
- User relationship
- Category relationship
Each post belongs to one user:The
user_id foreign key links posts to their authors.Eager loading relationships
You can load relationships efficiently using eager loading:app/Http/Controllers/PostController.php
Eager loading prevents N+1 query problems and improves performance.
Category model
The Category model represents blog post categories.app/Category.php
Category attributes
Categories have a simple structure:- id - Primary key (auto-increment)
- name - Category name (required)
- created_at - Timestamp when category was created
- updated_at - Timestamp when category was last updated
Category relationships
A category can have many posts:app/Http/Controllers/PostController.php
Eloquent relationships explained
The API uses two types of Eloquent relationships:Relationship diagram
Querying with relationships
Here are common patterns for working with these models:- Find posts by user
- Find posts by category
- Create post with relationships
Model validation
The API validates model data before saving:Next steps
Authentication
Learn how user authentication works with JWT tokens
API endpoints
Explore available endpoints for working with these models