File Uploads (Polymorphic)

A polymorphic file upload system where any model can have attached files. Uses Laravel morphs for the polymorphic relationship.

ERD Concept

Polymorphic: Files can belong to any model (posts, users, products). The files table stores fileable_type and fileable_id.

Tables

files

id fileable_type fileable_id user_id (FK) path original_name mime_type size disk timestamps

Relationships

  • File morphTo (fileable)
  • File belongsTo User
  • Post morphMany File
  • User morphMany File

Migration

Migration
Schema::create('files', function (Blueprint $table) {
    $table->id();
    $table->morphs('fileable'); // fileable_type, fileable_id + index
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->string('path');
    $table->string('original_name');
    $table->string('mime_type');
    $table->unsignedBigInteger('size');
    $table->string('disk')->default('public');
    $table->timestamps();
    $table->index(['fileable_type', 'fileable_id']);
});