【Laravel】artisanのmake:notificationコマンドを解説

チートシート

Laravel 10.xのartisanの「make:notification」コマンドを解説します。

Description

新しい通知クラスを作成します。

Usage

php artisan make:notification [-f|--force] [-m|--markdown [MARKDOWN]] [--test] [--pest] [--] <name>

Arguments

引数必須設営
name必須通知クラス名

Options

オプション省略形必須説明
forcef-通知クラスが既に存在する場合でも、クラスを作成
markdownm-通知用のMarkdownテンプレートを新規作成
test--通知クラスに付随するPHPUnitのテストを生成
pest--通知クラスに付随するPestテストを生成

Examples

no options

php artisan make:notification Foo
   INFO  Notification [app/Notifications/Foo.php] created successfully.  
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class Foo extends Notification
{
    use Queueable;

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

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}

Stub

OptionStub file name
-notification.stub
markdownmarkdown-notification.stub

Source

10.x

framework/src/Illuminate/Foundation/Console/NotificationMakeCommand.php at 10.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

11.x

framework/src/Illuminate/Foundation/Console/NotificationMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

コメント

タイトルとURLをコピーしました