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