The purpose of this package is to save, update, delete, force delete, and restore data using a recursive array. It understands both an associative array for working with one record, and with a dataset for working with several records at once.
You can install the library using composer:
composer require bfg/bless_model
First, you need to design your models and their relationships.
For convenience, the main method has been brought into the alias BlessModel
.
All you need to do is call the do
method.
\BlessModel::do(\App\Models\User::class, []);
Let's imagine that our user model has the following relationships:
class Profile extends \Illuminate\Database\Eloquent\Model {
protected $fillable = ["first_name","last_name","age","about"];
}
class Role extends \Illuminate\Database\Eloquent\Model {
protected $fillable = ["slug","name"];
}
class Post extends \Illuminate\Database\Eloquent\Model {
protected $fillable = ["subject","text"];
}
class Commentary extends \Illuminate\Database\Eloquent\Model {
protected $fillable = ["text"];
}
And in order to fill in this data, you need to do the following:
\BlessModel::do(\App\Models\User::class, [
"name" => "DoctorWho",
"email" => "[email protected]",
"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!"
]
]
]);