PHPackages                             wardenyarn/model-properties - 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. [Database &amp; ORM](/categories/database)
4. /
5. wardenyarn/model-properties

ActiveLibrary[Database &amp; ORM](/categories/database)

wardenyarn/model-properties
===========================

Add properties to your Laravel model

1.0.0(4y ago)04MITPHPPHP ^7.4|^8.0

Since Oct 7Pushed 4y ago1 watchersCompare

[ Source](https://github.com/three-cos/laravel-model-properties)[ Packagist](https://packagist.org/packages/wardenyarn/model-properties)[ Docs](https://github.com/wardenyarn/model-properties)[ RSS](/packages/wardenyarn-model-properties/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Laravel Model Properties
========================

[](#laravel-model-properties)

Simple model properties implementation.

### Problem

[](#problem)

Imagine, you have a Page model with given schema

```
Schema::create('pages', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug');
    $table->text('body');
    $table->timestamps();
});
```

But then, you might decide to add a meta\_title property. Or another bunch of properties, that might be useful for just one or two pages. In the end, we will see something like that:

```
Schema::create('pages', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug');
    $table->text('body');
    $table->boolean('has_halloween_theme');
    $table->string('meta_title');
    $table->string('meta_description');
    $table->integer('number_of_external_links');
    $table->boolean('export_to_rss');
    ...
    $table->timestamps();
});
```

### Solution

[](#solution)

To avoid extending of original table, we can use a one-to-one relation. But you will need to create additional table for every model with Properties. So, we might just use a polymorphic relation for properties and models.

```
Schema::create('property_values', function (Blueprint $table) {
    $table->id();
    $table->morphs('entity');
    $table->morphs('property');
    $table->text('value')->nullable();
    $table->timestamps();
});
```

This package is made to simplify this polymorphic relation setup.

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

[](#installation)

You can install the package via composer:

```
composer require wardenyarn/model-properties
```

Usage
-----

[](#usage)

First we need to publish a properties table and migrate it.

```
php artisan vendor:publish --provider="Wardenyarn\Properties\PropertiesServiceProvider" --tag="migrations"

php artisan migrate
```

Second, we need to add `HasProperties` trait and fill protected `$properties` property.

```
