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.
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.
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
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.