andyantunes/helper-commands

Helper to automate the creation of observers to Models

Installs: 99

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/andyantunes/helper-commands

v2.0.4 2024-10-30 19:24 UTC

README

Commands to help in the development of laravel projects

Latest Stable Version Total Downloads PHP Version Require License

Table of Contents

Installation

Install the package via composer

composer require andyantunes/helper-commands --dev

Publish the migration to use the Log Activities and the config/helper-commands.php config file

php artisan vendor:publish --provider="AndyAntunes\\HelperCommands\\HelperCommandsServiceProvider"

Before running migration

  • Check the configuration file config/helper-commands.php and change the configurations if you need

Run the migration

php artisan migrate

Activity Log

To generate the classes and methods use this command

php artisan helper:activity

and select the Model which you use to generate the Observer.

Observer

The observer class is generated with the following methods

public function created(MyModel $myModel): void
{
    $action = "Criou o myModel de ID: {$myModel->id}";
    LogActivity::setAction($action)
        ->create();
}
public function updated(MyModel $myModel): void
{
    $action = "Atualizou o myModel de ID: {$myModel->id}";
    LogActivity::setAction($action)
        ->create();
}
public function deleted(MyModel $myModel): void
{
    $action = "Deletou o myModel de ID: {$myModel->id}";
    LogActivity::setAction($action)
        ->create();
}
public function restored(MyModel $myModel): void
{
    $action = "Restaurou o myModel de ID: {$myModel->id}";
    LogActivity::setAction($action)
        ->create();
}
public function forceDeleted(MyModel $myModel): void
{
    $action = "Removeu o myModel de ID: {$myModel->id}";
    LogActivity::setAction($action)
        ->create();
}

Factories

To generate factories based in your table use this command

php artisan helper:factory

and select options like ModelName, quantity of records and whether to have events withEvents

Example of the created Factory

User::withoutEvents(function () {
    $this->command->warn(PHP_EOL . 'Creating users...');

    $this->withProgressBar(7, fn () => User::factory(1)->create());

    $this->command->info('Users created.');
});

License

The MIT License (MIT). Please see License File for more information.