LaravelPackages.net
Acme Inc.
Toggle sidebar
bfg/bless_model

Recursive auto save eloquent Models

87
1
1.0.0
About bfg/bless_model

bfg/bless_model is a Laravel package for recursive auto save eloquent models. It currently has 1 GitHub stars and 87 downloads on Packagist (latest version 1.0.0). Install it with composer require bfg/bless_model. Discover more Laravel packages by bfg or browse all Laravel packages to compare alternatives.

Last updated

BlessModel

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" => "[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!"
        ]
    ]
]);
Comments