bfg / bless_model
递归自动保存eloquent Models
0.1.0
2022-04-04 07:21 UTC
Requires
- php: ^8.0
- bfg/object: *
- bfg/text: *
- illuminate/contracts: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-09-04 12:54:54 UTC
README
此包的目的是使用递归数组保存、更新、删除、强制删除和恢复数据。它理解关联数组用于处理单个记录,以及数据集用于一次性处理多个记录。
安装
您可以使用composer安装此库
composer require bfg/bless_model
工作开始
首先,您需要设计您的模型及其关系。
如何使用
为了方便,主方法已经被引入为别名 BlessModel
。您只需要调用 do
方法。
\BlessModel::do(\App\Models\User::class, []);
让我们假设我们的用户模型有以下关系
profile - hasOne
class Profile extends \Illuminate\Database\Eloquent\Model { protected $fillable = ["first_name","last_name","age","about"]; }
roles - belongsToMany
class Role extends \Illuminate\Database\Eloquent\Model { protected $fillable = ["slug","name"]; }
posts - hasMany
class Post extends \Illuminate\Database\Eloquent\Model { protected $fillable = ["subject","text"]; }
commentaries - morphMany
class Commentary extends \Illuminate\Database\Eloquent\Model { protected $fillable = ["text"]; }
为了填充这些数据,您需要执行以下操作
\BlessModel::do(\App\Models\User::class, [ "name" => "DoctorWho", "email" => "user@test.com", "profile" => [ "first_name" => "Doctor", "last_name" => "Who", "age" => 2200, "about" => "An eccentric alien traveler of a great mind who fights injustice." ], "role" => [ "slug" => "time_lord", "name" => "Time Lord", ], "posts" => [ [ "subject" => "About TARDIS", "text" => "The TARDIS (Time And Relative Dimension In Space) is a time machine and spacecraft that appears in the British science fiction television series Doctor Who and its various spin-offs." ] ], "commentaries" => [ [ "text" => "Butterflies are cool!" ], [ "text" => "Yowzah!" ], [ "text" => "Geronimo!" ] ] ]);