masterfri / laravel-smart-relations
像操作属性一样简单地进行关系操作。
v1.2
2019-02-27 12:21 UTC
Requires
- php: >=7.1.0
- illuminate/database: ~5.3
Requires (Dev)
- orchestra/database: ~3.6
- orchestra/testbench: ~3.7
- phpunit/phpunit: ^7.0
This package is not auto-updated.
Last update: 2024-09-19 15:40:40 UTC
README
这个库允许以属性的方式处理关系。要创建关系,只需为模型分配一个值(或多个值,在批量赋值的情况下)并创建/保存它。
目前支持的以下关系类型:BelongsTo, BelongsToMany, HasOne, HasMany, MorphTo, MorphToMany, MorphOne, MorphMany。
安装
composer require masterfri/laravel-smart-relations
示例
use Masterfri\SmartRelations\SmartRelations; class Library extends Model { // To enable smart relations add the following trait in your model use SmartRelations; // Relations can be listed in $fillable property to enable mass assignment protected $fillable = ['name', 'books', 'readers']; // If you want children records to be deleted along with parent record // you can list required relation names in $cascade_delete property. // For example, when library instance is deleted all related books // will be deleted as well, and readers will be detached (not deleted, since // it is BelongsToMany relationship) protected $cascade_delete = ['books', 'readers']; public function books() { return $this->hasMany(Book::class); } public function readers() { return $this->belongsToMany(Reader::class, 'subscriptions'); } } class Book extends Model { use SmartRelations; protected $fillable = ['title', 'library']; public function library() { return $this->belongsTo(Library::class); } public function card() { return $this->hasOne(BookCard::class); } } class Reader extends Model { use SmartRelations; protected $fillable = ['name']; } class BookCard extends Model { use SmartRelations; protected $fillable = ['book', 'reader']; public function book() { return $this->belongsTo(Book::class); } public function reader() { return $this->belongsTo(Reader::class); } } // HasMany relation example // ========================= $library = Library::create([ 'name' => 'Central Library', // Related records can be passed as array or model instance // If instances does not exist in database, they will be created // You also can pass ID of existing record to attach it 'books' => [ ['title' => 'First Book'], new Book(['title' => 'Second Book']), $existingBook, 123, ], ]); // or $library->books = [ ['title' => 'First Book'], new Book(['title' => 'Second Book']), $existingBook, 123, ]; $library->save(); // HasOne relation example // ========================= $book = Book::create([ 'title' => 'Third book', 'card' => new BookCard(), // or simply // 'card' => [], ]); // BelongsTo relation example // ========================= $book = Book::create([ 'title' => 'Third book', // In that way model can be associated with parent record 'library' => $library, ]); // or $book->library = $library; $book->save(); // BelongsToMany relation example // ========================= $reader1 = Reader::create(['name' => 'John']); $reader2 = Reader::create(['name' => 'Jack']); // In case of many to many relation you can pass model instance/ID to make relationship $library->readers = [$reader1, $reader2->id]; $library->save(); // Update data on related records // ========================= // If you pass primary key in the data, related records will be loaded // from database, and their attributes will be updated. But bear in mind // that related records which are not listed in the array will be deleted, // because all children records will be replaced with new data. $library->books = [ ['id' => 1, 'title' => 'First Book New Name'], ['id' => 2, 'title' => 'Second Book New Name'], ];