dex/anything

It's can be anything

dev-main 2024-05-08 18:48 UTC

This package is auto-updated.

Last update: 2024-05-08 18:48:22 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

You always need to create auxiliary tables in your database to store few records just to have good database normalization, with anything package this will finish.

Installation

You can install the package via composer:

composer require dex/anything

Now, you can create any virtual/logical model using Anything model or extended it.

Belongs to relation

Suppose you have a Person model that have a belongsTo relation with Gender, Race and Religion model, you should have something like this for you models using 4 database tables.

class Person extends Model
{
    protected $table = 'person';
     
    public function gender(): BelongsTo
    {
        return $this->belongsTo(Gender::class);
    }
    
    public function race(): BelongsTo
    {
        return $this->belongsTo(Race::class);
    }
    
    public function religion(): BelongsTo
    {
        return $this->belongsTo(Religion::class);
    }
}

class Gender extends Model
{
    protected $table = 'gender';
}

class Race extends Model
{
    protected $table = 'race';
}

class Religion extends Model
{
    protected $table = 'religion';
}

For each model you will have a database table.

But using Anything model you will have just 2 database tables.

class Person extends Model
{
    protected $table = 'person';
     
    public function gender(): BelongsTo
    {
        return $this->belongsTo(Gender::class);
    }
    
    public function race(): BelongsTo
    {
        return $this->belongsTo(Race::class);
    }
    
    public function religion(): BelongsTo
    {
        return $this->belongsTo(Religion::class);
    }
}

class Gender extends Anything
{
}

class Race extends Anything
{
}

class Religion extends Anything
{
}

And you still have access through Eloquent relationships normally

$person = Person::factory()->create();

$person->gender->label; // Ex.: Female
$person->race->label; // Ex.: Black
$person->religion->label; // Ex.: Atheist

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.