PHP code example of edulazaro / laractions

1. Go to this page and download the library: Download edulazaro/laractions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edulazaro / laractions example snippets


namespace App\Actions;

use EduLazaro\Laractions\Action;

class SendEmail extends Action
{
    public function handle()
    {
        // Your action logic here
    }
}

namespace App\Actions;

use EduLazaro\Laractions\Action;

class SendEmail extends Action
{
    public function handle(string $email, string $subject, string $message)
    {
        // Your action logic here
    }
}

SendEmail::create()->run('[email protected]', 'Welcome!', 'Hello User');

// Positional
SendEmail::create()->run('[email protected]', 'Welcome!', 'Hello User');

// Named arguments
SendEmail::create()->run(email: '[email protected]', subject: 'Welcome!', message: 'Hello User');

// Associative array, mapped to parameters by name
SendEmail::create()->run([
    'email' => '[email protected]',
    'subject' => 'Welcome!',
    'message' => 'Hello User',
]);

class CreateInvoice extends Action
{
    public function handle(array $attributes)
    {
        // $attributes === ['concept' => 'x', 'amount' => 10]
        return $attributes['concept'];
    }
}

CreateInvoice::create()->run(['concept' => 'x', 'amount' => 10]);

ProcessFile::create()->run(['file' => $file]);  // mapped by name -> $file = $file
ProcessFile::create()->run($file);              // positional     -> $file = $file

namespace App\Actions;

use EduLazaro\Laractions\Action;
use App\Services\MailerService;

class SendEmail extends Action
{
    protected MailerService $mailer;

    /**
     * Inject dependencies via the constructor.
     */
    public function __construct(MailerService $mailer)
    {
        $this->mailer = $mailer;
    }

    /**
     * Handle the action logic.
     */
    public function handle(string $email, string $subject, string $message)
    {
        // Use the injected service
        $this->mailer->send($email, $subject, $message);
    }
}

SendEmail::create()->run('[email protected]', 'Welcome!', 'Hello User');

namespace App\Actions\User;

use EduLazaro\Laractions\Action;
use App\Models\User;

class SendEmail extends Action
{
    protected User $user;

    public function handle()
    {
        // Implement action logic for user here
    }
}

class User extends Model
{
    use HasActions;

    protected array $actions = [
        'send_email' => SendEmail::class,
    ];
}

$user->action('send_email')->run('[email protected]', 'Welcome!', 'Hello User');

$user->action(SendEmail::class)->run('[email protected]', 'Welcome!', 'Hello User');


$action = SendEmail::create()->with([
    'email' => '[email protected]',
    'subject' => 'Welcome!',
    'message' => 'Hello User'
])->run();

$this->email;

$user->action(SendEmail::class)->run();

class SendEmail extends Action
{
    protected User $user; // Automatically injected

    public function handle()
    {
        Mail::to($this->user->email)->send(new WelcomeMail());
    }
}

$action = SendEmail::create()
    ->queue('high')
    ->delay(10)
    ->retry(5)
    ->dispatch('[email protected]', 'Welcome!', 'Hello User');

class SendEmail extends Action
{
    protected int $tries = 5;
    protected ?int $delay = 30;
    protected ?string $queue = 'emails';
}

$user->mockAction(SendEmail::class, new class {
    public function run()
    {
        return 'Mocked!';
    }
});

echo $user->action(SendEmail::class)->run(); // Output: 'Mocked!'

php artisan list:actions

SendEmail::create()
    ->enableLogging()
    ->run('[email protected]', 'Welcome!', 'Hello User');

use EduLazaro\Laractions\Concerns\IsActor;

class User extends Model
{
    use IsActor;
}

$user->act(SendInvoice::class)
     ->on($order)
     ->trace()
     ->run();

SendEmail::create()
    ->trace()
    ->run('[email protected]', 'Welcome!', 'Hello!');

SendEmail::create()
    ->actor($user)
    ->on($targetModel)
    ->trace()
    ->run($params);

$user->act(SendInvoice::class)
     ->on($order)
     ->trace()
     ->run();
bash
php artisan make:action SendEmail        # class SendEmail
php artisan make:action SendEmailAction  # class SendEmailAction
bash
php artisan make:action SendEmail
bash
php artisan make:action SendEmail --model=User