Skip to main content
MyDiary uses MySQL as its primary database. The schema is managed through Laravel migrations located in database/migrations/.

Schema overview

The database consists of the following core tables:
  • users - User accounts and profiles
  • entries - Diary entries
  • friend_requests - Friend request management
  • entry_users - Shared diary entries (many-to-many)
  • image_users - User profile pictures
  • image_entries - Entry image attachments
  • likes - Entry likes/reactions
  • sessions - Active user sessions
  • password_reset_tokens - Password reset functionality

Entity relationship diagram

┌──────────┐       ┌───────────────┐       ┌──────────┐
│  users   │──────<│friend_requests│>──────│  users   │
└────┬─────┘       └───────────────┘       └──────────┘

     │ 1:N

┌────▼─────────┐
│image_users   │
└──────────────┘

     │ 1:N

┌────▼─────┐       ┌──────────────┐
│ entries  │──────<│ entry_users  │>────── users
└────┬─────┘       └──────────────┘

     │ 1:N

     ├──────────┐
     │          │
┌────▼────────┐ │
│image_entries│ │
└─────────────┘ │

           ┌────▼────┐
           │  likes  │
           └─────────┘

Tables

users

Stores user account information and profile settings. Migration: 0001_01_01_000000_create_users_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTUser unique identifier
namevarchar(255)NOT NULLUser’s display name
emailvarchar(255)NOT NULL, UNIQUEUser’s email address
passwordvarchar(255)NOT NULLHashed password
adminbooleanDEFAULT falseAdmin privilege flag
colorConfigurationvarchar(255)NULLABLECustom color theme preference
created_attimestampNULLABLEAccount creation timestamp
updated_attimestampNULLABLELast update timestamp
Relationships:
  • Has many entries (as creator)
  • Has many friend_requests (as sender or receiver)
  • Has many image_users (profile pictures)
  • Has many likes
  • Belongs to many entries through entry_users (shared entries)

entries

Stores diary entry content and metadata. Migration: 2025_05_21_013634_create_entries_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTEntry unique identifier
bodytextNOT NULLEntry content/text
visibilityvarchar(255)NOT NULLVisibility level: ‘public’, ‘private’, or ‘friends’
creator_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEEntry author
created_attimestampNULLABLEEntry creation timestamp
updated_attimestampNULLABLELast update timestamp
Indexes:
  • Foreign key on creator_id
Relationships:
  • Belongs to users (creator)
  • Has many image_entries
  • Has many likes
  • Belongs to many users through entry_users (shared with)
Visibility levels:
  • private - Only visible to creator
  • public - Visible to everyone
  • friends - Visible to friends only

friend_requests

Manages friend connections between users. Migration: 2025_05_21_013616_create_friend_requests_table.php (updated by 2025_07_12_223135_add_change_variables_in_friend_requests.php)
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTRequest unique identifier
statusvarchar(255)NOT NULLRequest status: ‘pending’, ‘accepted’, or ‘rejected’
send_atdatetimeNOT NULLRequest sent timestamp
response_atdatetimeNULLABLERequest response timestamp
sender_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEUser who sent request
recived_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEUser who received request
Indexes:
  • Foreign key on sender_id
  • Foreign key on recived_id
Relationships:
  • Belongs to users (sender)
  • Belongs to users (receiver)
Status values:
  • pending - Awaiting response
  • accepted - Friend request accepted
  • rejected - Friend request rejected

entry_users

Pivot table for sharing diary entries with specific users. Migration: 2025_05_21_013654_create_entry_users_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTRelation unique identifier
entry_idbigint unsignedFOREIGN KEY → entries.id, ON DELETE CASCADEShared entry
user_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEUser with access
created_attimestampNULLABLEShare timestamp
updated_attimestampNULLABLELast update timestamp
Indexes:
  • Foreign key on entry_id
  • Foreign key on user_id
Relationships:
  • Belongs to entries
  • Belongs to users

image_users

Stores user profile pictures. Migration: 2025_05_21_013722_create_image_users_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTImage unique identifier
namevarchar(255)NOT NULLImage filename
pathvarchar(255)NOT NULLStorage path
selectedbooleanDEFAULT trueCurrently active profile picture
user_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEImage owner
created_attimestampNULLABLEUpload timestamp
updated_attimestampNULLABLELast update timestamp
Indexes:
  • Foreign key on user_id
Relationships:
  • Belongs to users
Note: Users can have multiple profile pictures, but only one is selected at a time.

image_entries

Stores images attached to diary entries. Migration: 2025_05_21_013732_create_image_entries_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTImage unique identifier
namevarchar(255)NOT NULLImage filename
pathvarchar(255)NOT NULLStorage path
entry_idbigint unsignedFOREIGN KEY → entries.id, ON DELETE CASCADEAssociated entry
created_attimestampNULLABLEUpload timestamp
updated_attimestampNULLABLELast update timestamp
Indexes:
  • Foreign key on entry_id
Relationships:
  • Belongs to entries

likes

Tracks user reactions/likes on diary entries. Migration: 2025_05_21_013929_create_likes_table.php
ColumnTypeConstraintsDescription
idbigint unsignedPRIMARY KEY, AUTO_INCREMENTLike unique identifier
typevarchar(255)NOT NULLReaction type (e.g., ‘like’, ‘love’)
entry_idbigint unsignedFOREIGN KEY → entries.id, ON DELETE CASCADELiked entry
user_idbigint unsignedFOREIGN KEY → users.id, ON DELETE CASCADEUser who liked
created_attimestampNULLABLELike timestamp
updated_attimestampNULLABLELast update timestamp
Indexes:
  • Foreign key on entry_id
  • Foreign key on user_id
Relationships:
  • Belongs to entries
  • Belongs to users

sessions

Laravel session storage table. Migration: 0001_01_01_000000_create_users_table.php
ColumnTypeConstraintsDescription
idvarchar(255)PRIMARY KEYSession unique identifier
user_idbigint unsignedNULLABLE, INDEXAuthenticated user ID
ip_addressvarchar(45)NULLABLEClient IP address
user_agenttextNULLABLEClient user agent
payloadlongtextNOT NULLSerialized session data
last_activityintegerNOT NULL, INDEXUnix timestamp of last activity
Indexes:
  • Index on user_id
  • Index on last_activity

password_reset_tokens

Stores password reset tokens. Migration: 0001_01_01_000000_create_users_table.php
ColumnTypeConstraintsDescription
emailvarchar(255)PRIMARY KEYUser email address
tokenvarchar(255)NOT NULLReset token hash
created_attimestampNULLABLEToken creation timestamp

Cascade behaviors

All foreign key relationships use ON DELETE CASCADE and ON UPDATE CASCADE, meaning:
  • Deleting a user will delete all their entries, friend requests, images, and likes
  • Deleting an entry will delete all associated images, likes, and sharing relationships
  • This maintains referential integrity automatically

Database conventions

Naming conventions

  • Table names: plural, snake_case (e.g., friend_requests)
  • Column names: snake_case (e.g., created_at)
  • Primary keys: id
  • Foreign keys: {table_singular}_id (e.g., user_id)
  • Pivot tables: alphabetical order (e.g., entry_users)

Timestamps

Most tables include Laravel’s automatic timestamps:
  • created_at - Set when record is created
  • updated_at - Updated when record is modified

Querying examples

Get all public entries with creator and images

$entries = Entry::with(['creator', 'image_entry'])
    ->where('visibility', 'public')
    ->orderBy('created_at', 'desc')
    ->get();

Get user’s friends

$friends = FriendRequest::where('status', 'accepted')
    ->where(function($query) use ($userId) {
        $query->where('sender_id', $userId)
              ->orWhere('recived_id', $userId);
    })
    ->get();

Get entries shared with a user

$sharedEntries = $user->entries()  // via entry_users pivot
    ->with(['creator', 'image_entry'])
    ->get();

Migration files

All migrations are located in database/migrations/:
  • 0001_01_01_000000_create_users_table.php
  • 2025_05_21_013616_create_friend_requests_table.php
  • 2025_05_21_013634_create_entries_table.php
  • 2025_05_21_013654_create_entry_users_table.php
  • 2025_05_21_013722_create_image_users_table.php
  • 2025_05_21_013732_create_image_entries_table.php
  • 2025_05_21_013929_create_likes_table.php
  • 2025_07_12_223135_add_change_variables_in_friend_requests.php

Next steps

Build docs developers (and LLMs) love