PHP code example of lorisleiva / laravel-actions

1. Go to this page and download the library: Download lorisleiva/laravel-actions 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/ */

    

lorisleiva / laravel-actions example snippets


PublishANewArticle::run($author, 'My title', 'My content');

Route::post('articles', PublishANewArticle::class)->middleware('auth');

Event::listen(NewProductReleased::class, PublishANewArticle::class);

event(new NewProductReleased($manager, 'Product title', 'Product description'));
 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,
        );
    }
}