akaadream/extendedmodel

ExtendedModel - A better experience with Laravel models

v1.1.0 2024-04-20 10:19 UTC

This package is not auto-updated.

Last update: 2024-05-04 10:47:31 UTC


README

Latest stable version

Laravel ExtendedModel is a package to upgrade the possibilities of the default Eloquent Model. ExtendedModel provide a new method called createOrUpdateWith which is using request inputs to hydrate a model instance.

No more big array of model properties' initialization !

Installation

You can install the package using composer:

composer require akaadream/extendedmodel

Usage

You should replace the default command php artisan make:model with php artisan make:extendedmodel to quickly create an extended model. For example, php artisan make:extendedmodel MyModel will give you this class:

use \Akaadream\ExtendedModel\ExtendedModel;

class MyModel extends ExtendedModel
{
    //
}

You will now be able to use the createOrUpdateWith method inside your controllers:

// ...

public function store (Request $request)
{
    MyModel::createOrupdateWith(new MyModel, $request);
}

Take in consideration that all the requested inputs have to follow the exact same name of the model properties. So if your model have a property name, the method will try to find $request->input('name') to assign its value.

You can also put an array of options at the third parameter of the method if you want to attribute a specific value for any of the model property. For example, if you have to upload a file, and then, you want to put on your model the filename inside an image property, you can do:

public function store (Request $request)
{
    $filename = "extendedmodelimage.png";
    // ... image upload

    MyModel::createOrUdateWith(new MyModel, $request, ['image' => $filename]);
}