PHP

Handling Flexible Associations with Eloquent Polymorphic Relationships

Implement polymorphic relationships in Laravel Eloquent to allow a single model to belong to multiple different models on a single association column, offering high flexibility.

// Database Migrations (Example for images table):
// Schema::create('images', function (Blueprint $table) {
//     $table->id();
//     $table->string('url');
//     $table->morphs('imageable'); // Adds imageable_id (INT) and imageable_type (VARCHAR)
//     $table->timestamps();
// });

// In App\Models\Image.php (The 'morphing' model)
class Image extends Model
{
    protected $fillable = ['url'];

    public function imageable()
    {
        return $this->morphTo(); // Defines the polymorphic relationship
    }
}

// In App\Models\Post.php (One of the 'owners')
class Post extends Model
{
    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }
}

// In App\Models\Video.php (Another 'owner')
class Video extends Model
{
    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }
}

// Usage:
$post = App\Models\Post::find(1);
$post->images()->create(['url' => 'post-header.jpg']);

$video = App\Models\Video::find(5);
$video->images()->create(['url' => 'video-thumbnail.png']);

// Accessing the owner of an image
$image = App\Models\Image::find(1);
$imageable = $image->imageable; // Returns either a Post model or a Video model based on imageable_type

echo $imageable::class; // e.g., "App\Models\Post"
How it works: Polymorphic relationships allow a model to belong to more than one other model on a single association. For example, an `Image` model might belong to a `Post` model or a `Video` model. Laravel handles this by adding two columns to the 'morphing' table (e.g., `images`): an `_id` column (e.g., `imageable_id`) to store the ID of the parent model, and an `_type` column (e.g., `imageable_type`) to store the class name of the parent model. This offers immense flexibility when you have a common resource that can be attached to various types of entities.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs