All Projects → paulhenri-l → laravel-sti

paulhenri-l / laravel-sti

Licence: MIT license
Single table inheritance with Laravel/Eloquent

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-sti

laravel-simplegrid
A simple component for generating powerful grids with Laravel.
Stars: ✭ 35 (+118.75%)
Mutual labels:  eloquent
laravel-cachable-attributes
Allows to cache attribute accessor values in an easy way.
Stars: ✭ 24 (+50%)
Mutual labels:  eloquent
LazyBelongsToMany
A lightweight implementation of Laravel's belongs To many
Stars: ✭ 23 (+43.75%)
Mutual labels:  eloquent
slim-boilerplate
A PHP boilerplate,for a fast API prototyping based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready
Stars: ✭ 58 (+262.5%)
Mutual labels:  eloquent
acorn-db
Provides Acorn projects with Eloquent Models for WordPress data.
Stars: ✭ 30 (+87.5%)
Mutual labels:  eloquent
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (+81.25%)
Mutual labels:  eloquent
inertiajs-tables-laravel-query-builder
Inertia.js Tables for Laravel Query Builder
Stars: ✭ 391 (+2343.75%)
Mutual labels:  eloquent
laravel-auto-morph-map
THIS PACKAGE HAS BEEN DEPRECATED — Automatically alias and map the polymorphic types of Eloquent models.
Stars: ✭ 55 (+243.75%)
Mutual labels:  eloquent
state-machine
The hyn state machine package is a flexible library that helps you move Eloquent models from States through Transitions while emitting events along the way.
Stars: ✭ 14 (-12.5%)
Mutual labels:  eloquent
query-filter
🔍 Database/Eloquent Query Builder filters for Laravel
Stars: ✭ 69 (+331.25%)
Mutual labels:  eloquent
encryptable
Laravel package for persisting encrypted Model properties, providing decryption when accessed.
Stars: ✭ 26 (+62.5%)
Mutual labels:  eloquent
eloquent-filter
Library to form search criteria through expressions in the query string
Stars: ✭ 23 (+43.75%)
Mutual labels:  eloquent
eloquent-hashids
Automatically generate and persist Hashids for newly created Eloquent models.
Stars: ✭ 17 (+6.25%)
Mutual labels:  eloquent
plug
A collection of pluggable traits for Eloquent (Laravel) models
Stars: ✭ 13 (-18.75%)
Mutual labels:  eloquent
Laravel-Auto-Hard-Deleter
Laravel and Lumen Auto Hard Deleter
Stars: ✭ 34 (+112.5%)
Mutual labels:  eloquent
eloquent-filemaker
A Model extension and Eloquent driver for Laravel connecting to FileMaker through the Data API
Stars: ✭ 38 (+137.5%)
Mutual labels:  eloquent
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+412.5%)
Mutual labels:  eloquent
laravel-hasmanywithinverse
Define HasMany while also setting the inverse relationship in Laravel.
Stars: ✭ 57 (+256.25%)
Mutual labels:  eloquent
db-safedelete
Attempts to invoke force delete, if it fails - falls back to soft delete
Stars: ✭ 16 (+0%)
Mutual labels:  eloquent
laravel-scoped-cache
Easily cache items specific to your Eloquent models without having to append the ID.
Stars: ✭ 28 (+75%)
Mutual labels:  eloquent

Travis

Laravel STI

This is my take at bringing Single Table Inheritance (STI) to the Eloquent ORM.

This package can be used outside of a laravel app

Installation

composer require phl/laravel-sti

Usage

Besides using the trait and adding the type column there is nothing much to do.

class Member extends Illuminate\Database\Eloquent\Model
{
    use PHL\LaravelSTI\STI;
}
Schema::create('members', function ($table) {
    // ...
    $table->type();
    // ...
});

You can now extend the Member model.

class PremiumMember extends Member
{
    //
}

class RegularMember extends Member
{
    //
}

And enjoy single table inheritance!

Configuration

Out of the box there is absolutely nothing to configure. You may want to change the defaults though.

Type column

By default the type column is named type if you want to use another name you can specify it in the migration and in the model.

class Member extends Illuminate\Database\Eloquent\Model
{
    use PHL\LaravelSTI\STI;

    protected static $stiTypeKey = 'custom_type_column'
}
Capsule::schema()->create('members', function ($table) {
    // ...
    $table->type('custom_type_column');
    // ...
});

Type value

If you do not want your type column to contain the class name you can use Eloquent's Relation::morphMap() function to add mapping between a name and a class.

Relation::morphMap([
    'regular_member' => RegularMember::class,
]);

Now the type column will be filled with regular_member instead of Member. This helps avoid leaking code details into the DB.

Read the source Luke!

If you are currious about the implementation details, the code and tests have been heavily documented :)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].