igaster/eloquent-inheritance 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

igaster/eloquent-inheritance

Composer 安装命令:

composer require igaster/eloquent-inheritance

包简介

Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models

README 文档

README

Laravel License Downloads Build Status Codecov

Description

Eloquent Multiple Table Inheritance: Use a One-To-One (or One-To-Many) relation between a parent & child Tables to simulate inheritance between Models.

Installation:

Edit your project's composer.json file to require:

"require": {
    "igaster/eloquent-inheritance": "~1.0"
}

and install with composer update

Usage:

Example Schema:

// Model Foo is the parent model
Schema::create('foo', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('a');
});

// Model Bar inherits Foo.
Schema::create('bar', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('b');
    $table->integer('foo_id')->nullable(); // Foreign Key to Foo
});

Example Models:

Your models should use the EloquentInherited Trait

class Foo extends Eloquent
{
    use \igaster\EloquentInheritance\EloquentInherited;

	// ...
    public function fooMethod(){}
}

class Bar extends Eloquent
{
    use \igaster\EloquentInheritance\EloquentInherited;

	// ...
    public function barMethod(){}
}

class BarExtendsFoo extends igaster\EloquentInheritance\InheritsEloquent{
    // Set Parent/Child classes
    public static $parentClass = Foo::class;
    public static $childClass  = Bar::class;

    // You must declare Parent/Child keys explicity:
    public static $parentKeys = ['id','a'];
    public static $childKeys  = ['id','b','foo_id'];

    // Childs Foreign Key (points to Parent)
    public static $childFK  = 'foo_id';

    // You can add your functions / variables ...
    public function newMethod(){}
}

Usage:

// Create a composite Model:
$fooBar = BarExtendsFoo::create([ // Creates and Saves Foo & Bar in the Database
    'a' => 1,
    'b' => 2,
]);

// Access Attributes:
$fooBar->a; // = 1 (from Foo model)
$fooBar->b; // = 2 (from Bar model)

// Call Methods:
$fooBar->fooMethod(); // from Foo Model
$fooBar->barMethod(); // from Bar Model
$fooBar->newMethod(); // from self

// Query as an Eloquent model:
$fooBar = BarExtendsFoo::find(1);
$fooBar = BarExtendsFoo::where('a',1)->first();
$fooBar = BarExtendsFoo::get();     // Collection of BarExtendsFoo 

$fooBar->save();    // Saves Foo & Bar
$fooBar->delete();  // Deletes Foo & Bar
$fooBar->update([   // Updates Foo & Bar
    'a' => 10,
    'b' => 20,
]);

统计信息

  • 总下载量: 150
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固