PHPackages                             techziondev/laravel-polymorphic-entities - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. techziondev/laravel-polymorphic-entities

ActiveLibrary

techziondev/laravel-polymorphic-entities
========================================

00PHP

Since Sep 11Pushed 7mo agoCompare

[ Source](https://github.com/techziondev/laravel-polymorphic-entities)[ Packagist](https://packagist.org/packages/techziondev/laravel-polymorphic-entities)[ RSS](/packages/techziondev-laravel-polymorphic-entities/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Polymorphic Entities
============================

[](#laravel-polymorphic-entities)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3c377617d8f7fe0f2f5ab87002aa249729a3b5affa8338fb8cf3def2520a73d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746563687a696f6e6465762f6c61726176656c2d706f6c796d6f72706869632d656e7469746965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/techziondev/laravel-polymorphic-entities)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3cea6135602c657eef15d96718983bf05da67ba482faa4cfcb9d61d512a9ad86/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f746563687a696f6e6465762f6c61726176656c2d706f6c796d6f72706869632d656e7469746965732f54657374733f6c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/techziondev/laravel-polymorphic-entities/actions)[![Total Downloads](https://camo.githubusercontent.com/10927f0299f27c993a089a63e868c8da263a661dcce17fd6a0c3c6f28ad9e6da/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746563687a696f6e6465762f6c61726176656c2d706f6c796d6f72706869632d656e7469746965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/techziondev/laravel-polymorphic-entities)

A powerful Laravel package that provides flexible polymorphic entity associations with time-based controls, metadata support, and a fluent API for managing complex model relationships.

Features
--------

[](#features)

- **Polymorphic Associations**: Associate any model with multiple types of entities
- **Time-Based Control**: Set start and expiration dates for associations
- **Metadata Support**: Store custom metadata with each association
- **Fluent API**: Intuitive builder pattern for managing associations
- **Event System**: Built-in events for association lifecycle management
- **Active Status Tracking**: Enable/disable associations without deletion
- **Automatic Cleanup**: Console command for removing expired associations

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 11.0 or 12.0
- Illuminate Database package

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require techziondev/laravel-polymorphic-entities
```

### Publish and Run Migrations

[](#publish-and-run-migrations)

Publish the migration file:

```
php artisan vendor:publish --provider="Techzion\LaravelPolymorphicEntities\LaravelPolymorphicEntitiesServiceProvider" --tag="migrations"
```

Run the migrations:

```
php artisan migrate
```

### Publish Configuration (Optional)

[](#publish-configuration-optional)

You can optionally publish the configuration file:

```
php artisan vendor:publish --provider="Techzion\LaravelPolymorphicEntities\LaravelPolymorphicEntitiesServiceProvider" --tag="config"
```

Basic Usage
-----------

[](#basic-usage)

### Setting Up Your Models

[](#setting-up-your-models)

#### 1. Models That Can Be Associated (HasEntities)

[](#1-models-that-can-be-associated-hasentities)

Add the `HasEntities` trait to models that can be associated with entities:

```
