PHPackages                             illuminatech/db-role - 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. illuminatech/db-role

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

illuminatech/db-role
====================

Provides support for Eloquent relation role (table inheritance) composition in Laravel

1.1.6(1y ago)1516BSD-3-ClausePHPCI passing

Since May 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/illuminatech/db-role)[ Packagist](https://packagist.org/packages/illuminatech/db-role)[ GitHub Sponsors](https://github.com/klimov-paul)[ Patreon](https://www.patreon.com/klimov_paul)[ RSS](/packages/illuminatech-db-role/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (9)Used By (0)

 [ ![](https://avatars1.githubusercontent.com/u/47185924) ](https://github.com/illuminatech)

Eloquent Role Inheritance Extension
===================================

[](#eloquent-role-inheritance-extension)

This extension provides support for Eloquent relation role (table inheritance) composition.

For license information check the [LICENSE](LICENSE.md)-file.

[![Latest Stable Version](https://camo.githubusercontent.com/1ff3df41779d3941a27b9b733346af310a2a1f34198f6da81a18a7f62581a290/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696c6c756d696e61746563682f64622d726f6c652e737667)](https://packagist.org/packages/illuminatech/db-role)[![Total Downloads](https://camo.githubusercontent.com/f6f12686f3d876a0a974e645cc6e983eafd0c58a775b50dd6d31257782a6a834/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696c6c756d696e61746563682f64622d726f6c652e737667)](https://packagist.org/packages/illuminatech/db-role)[![Build Status](https://github.com/illuminatech/db-role/workflows/build/badge.svg)](https://github.com/illuminatech/db-role/actions)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist illuminatech/db-role

```

or add

```
"illuminatech/db-role": "*"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

This extension provides support for Eloquent relation role composition, which is also known as table inheritance.

For example: assume we have a database for the University. There are students studying in the University, and there are instructors teaching the students. Student has a study group and scholarship information, while instructor has a rank and salary. However, both student and instructor have name, address, phone number and so on. Thus we can split their data in the three different tables:

- 'humans' - stores common data
- 'students' - stores student special data and reference to the 'humans' record
- 'instructors' - stores instructor special data and reference to the 'humans' record

DDL for such solution may look like following:

```
CREATE TABLE `humans`
(
   `id` integer NOT NULL AUTO_INCREMENT,
   `role` varchar(20) NOT NULL,
   `name` varchar(64) NOT NULL,
   `address` varchar(64) NOT NULL,
   `phone` varchar(20) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE InnoDB;

CREATE TABLE `students`
(
   `human_id` integer NOT NULL,
   `study_group_id` integer NOT NULL,
   `has_scholarship` integer(1) NOT NULL,
    PRIMARY KEY (`human_id`)
    FOREIGN KEY (`human_id`) REFERENCES `humans` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE InnoDB;

CREATE TABLE `instructors`
(
   `human_id` integer NOT NULL,
   `rank_id` integer NOT NULL,
   `salary` integer NOT NULL,
    PRIMARY KEY (`human_id`)
    FOREIGN KEY (`human_id`) REFERENCES `humans` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE InnoDB;
```

This extension introduces `\Illuminatech\DbRole\InheritRole` trait, which allows role relation based Eloquent inheritance. In order to make it work, first of all, you should create an Eloquent class for the base table, in our example it will be 'humans':

```
