PHP code example of ngmy / laravel-async-await-bus

1. Go to this page and download the library: Download ngmy/laravel-async-await-bus 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/ */

    

ngmy / laravel-async-await-bus example snippets




namespace App\Commands;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Ngmy\LaravelAsyncAwaitBus\Concerns\Respondable;
use Ngmy\LaravelAsyncAwaitBus\Contracts\ShouldAwaitResponse;

class CreateNewArticleCommand implements ShouldQueue, ShouldAwaitResponse
{
    use InteractsWithQueue, Queueable, SerializesModels, Respondable;

    public function __construct(
        public readonly User $user,
        public readonly string $title,
        public readonly string $body,
        public readonly bool $published,
    ) {
    }
}



namespace App\Handlers\Commands;

use App\Commands\CreateNewArticleCommand;

class CreateNewArticleCommandHandler
{
    public function handle(CreateNewArticleCommand $command): void
    {
        $article = $command->user->articles()->create([
            'title' => $command->title,
            'body' => $command->body,
            'published' => $command->published,
            'published_at' => $command->published ? now() : null,
        ]);

        $command->respond($article->id);
    }
}

use App\Commands\CreateNewArticleCommand;
use App\Handlers\Commands\CreateNewArticleCommandHandler;
use Illuminate\Contracts\Bus\Dispatcher as Bus;

$bus = $this->app->make(Bus::class);
$bus->map([
    CreateNewArticleCommand::class => CreateNewArticleCommandHandler::class,
]);



namespace App\Http\Controllers;

use App\Commands\CreateNewArticleCommand;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateNewArticleRequest;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Illuminate\Http\RedirectResponse;

class CreateNewArticle extends Controller
{
    public function __invoke(CreateNewArticleRequest $request, Bus $bus): RedirectResponse
    {
        $command = new CreateNewArticleCommand(
            $request->user(),
            $request->string('title'),
            $request->string('body'),
            $request->boolean('published'),
        );
        $id = $bus->dispatch($command);

        return redirect("articles/{$id}");
    }
}



namespace App\Commands;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Ngmy\LaravelAsyncAwaitBus\Concerns\Respondable;
use Ngmy\LaravelAsyncAwaitBus\Contracts\ShouldAwaitResponse;

class CreateNewArticleCommand implements ShouldQueue, ShouldAwaitResponse
{
    use InteractsWithQueue, Queueable, SerializesModels, Respondable;

    public function __construct(
        public readonly User $user,
        public readonly string $title,
        public readonly string $body,
        public readonly bool $published,
    ) {
    }

    public function handle(): void
    {
        $article = $this->user->articles()->create([
            'title' => $this->title,
            'body' => $this->body,
            'published' => $this->published,
            'published_at' => $this->published ? now() : null,
        ]);

        $this->respond($article->id);
    }
}