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
}
}
// 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);
}
}
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,
];
}
class SendEmail extends Action
{
protected User $user; // Automatically injected
public function handle()
{
Mail::to($this->user->email)->send(new WelcomeMail());
}
}
$user->mockAction(SendEmail::class, new class {
public function run()
{
return 'Mocked!';
}
});
echo $user->action(SendEmail::class)->run(); // Output: 'Mocked!'