jaspaul / eloquent-sti
A simple single table inheritance library for eloquent.
Installs: 6 741
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.2
- illuminate/database: ^6.0
- illuminate/support: ^6.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/database: ^4.0
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
- satooshi/php-coveralls: ^2.0
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2024-11-21 20:43:36 UTC
README
Install
Via Composer
$ composer require jaspaul/eloquent-sti
Requirements
The following versions of PHP are supported by this version.
- PHP 7.2
- PHP 7.3
- PHP 7.4
Usage
<?php use Tests\Helpers\User; use Tests\Helpers\Administrator; use Jaspaul\EloquentSTI\Inheritable; use Illuminate\Database\Eloquent\Model; class User extends Model { use Inheritable; /** * Provides a map of types to resolve for this object. The format is: * 'user' => User::class, * 'administrator' => Administrator::class * * @var array */ protected $types = [ 'user' => User::class, 'administrator' => Administrator::class ]; }
<?php class Administrator extends User { }
Now when you select users through the User model, they'll be returned with the associated type. For instance if you have a record in the users table with the type administrator, an Administrator object will be returned when you run User::where('type', 'administrator')->first()
.