■P201

────────────────
sail artisan make:job SampleJob
────────────────

■P202

●app/Jobs/SampleJob.php
────────────────
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SampleJob implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        //
    }
}
────────────────


■P203

●app/Jobs/SampleJob.php
────────────────
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SampleJob implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        echo 'Jobを実行しました。';
    }
}
────────────────

────────────────
sail artisan tinker
────────────────

────────────────
App\Jobs\SampleJob::dispatchSync();
────────────────


■P204

●.env
────────────────
QUEUE_CONNECTION=database
────────────────

●config/queue.php
────────────────
Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "deferred", "background", "failover", "null"
────────────────

────────────────
sail artisan tinker
────────────────

────────────────
\Bus::dispatch(new App\Jobs\SampleJob());
────────────────


■P205
────────────────
sail mysql
────────────────

────────────────
select * from jobs;
────────────────


■P206

────────────────
sail artisan queue:work
────────────────

────────────────
sail mysql
────────────────

────────────────
select * from jobs;
────────────────


■P207

────────────────
sail artisan make:job SendNewUserIntroductionJob
────────────────

●app/Jobs/SendNewUserIntroductionJob.php
────────────────
<?php

namespace App\Jobs;

use App\Mail\NewUserIntroduction;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendNewUserIntroductionJob implements ShouldQueue
{
		use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct(
        public User $newUser
    ) {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $allUsers = User::all();

        foreach ($allUsers as $user) {
            Mail::to($user->email)
                ->send(new NewUserIntroduction($user, $this->newUser));
        }
    }
}
────────────────


■P208

●app/Http/Controllers/Auth/Register/PostController.php
────────────────
<?php

namespace App\Http\Controllers\Auth\Register;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\RegisterRequest;
use App\Jobs\SendNewUserIntroductionJob;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class PostController extends Controller
{
    public function __invoke(RegisterRequest $request)
    {
        $newUser = User::create([
            'name' => $request->name(),
            'email' => $request->email(),
            'password' => Hash::make($request->password()),
        ]);

        Auth::login($newUser);

        // メール送信をJobでキューに積む
        SendNewUserIntroductionJob::dispatch($newUser);

        return redirect()->route('tweet.index');
    }
}
────────────────


■P209

────────────────
sail artisan queue:work
────────────────
