bfg/bless_model

Recursive auto save eloquent Models

1.0.0 2024-12-02 12:59 UTC

This package is auto-updated.

Last update: 2024-12-02 12:59:41 UTC


README

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.

Installation

You can install the library using composer:

composer require bfg/bless_model

Beginning of work

First, you need to design your models and their relationships.

How to use

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:

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"];
}

And in order to fill in this data, you need to do the following:

\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!"
        ]
    ]
]);