### Install Laravel Actions Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Installs the Laravel Actions package using Composer. ```bash composer require lorisleiva/laravel-actions ``` -------------------------------- ### Basic Action Usage in PHP Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Demonstrates how to create and use a basic Laravel Action. It includes a `handle` method for core logic and `asController` and `asListener` methods for different execution contexts. The `AsAction` trait is used to enable action functionality. ```php class PublishANewArticle { use AsAction; public function handle(User $author, string $title, string $body): Article { return $author->articles()->create([ 'title' => $title, 'body' => $body, ]); } public function asController(Request $request): ArticleResource { $article = $this->handle( $request->user(), $request->get('title'), $request->get('body'), ); return new ArticleResource($article); } public function asListener(NewProductReleased $event): void { $this->handle( $event->product->manager, $event->product->name . ' Released!', $event->product->description, ); } } ``` -------------------------------- ### Running Actions as Objects Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Shows how to execute a Laravel Action directly as an object using the `run` method, passing the required arguments. ```php PublishANewArticle::run($author, 'My title', 'My content'); ``` -------------------------------- ### Dispatching Events to Actions Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Shows how to dispatch an event that will trigger a registered Laravel Action listener. ```php event(new NewProductReleased($manager, 'Product title', 'Product description')); ``` -------------------------------- ### Registering Actions as Listeners Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Demonstrates how to register a Laravel Action as a listener for a specific event, allowing the action's `asListener` method to be called automatically. ```php Event::listen(NewProductReleased::class, PublishANewArticle::class); ``` -------------------------------- ### Registering Actions as Controllers Source: https://github.com/lorisleiva/laravel-actions/blob/main/README.md Illustrates how to register a Laravel Action as an invokable controller in a Laravel route definition. ```php Route::post('articles', PublishANewArticle::class)->middleware('auth'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.