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 SendEmailAction extends Action
{
    public function handle()
    {
        // Your action logic here
    }
}

namespace App\Actions;

use EduLazaro\Laractions\Action;

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

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

namespace App\Actions;

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

class SendEmailAction 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);
    }
}

SendEmailAction::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' => SendEmailAction::class,
    ];
}

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

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


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

$this->email;

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

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

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

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

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

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

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

php artisan list:actions

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

use EduLazaro\Laractions\Concerns\IsActor;

class User extends Model
{
    use IsActor;
}

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

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

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

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