{"id":1076,"date":"2024-03-03T21:28:12","date_gmt":"2024-03-03T12:28:12","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1076"},"modified":"2024-03-03T21:28:13","modified_gmt":"2024-03-03T12:28:13","slug":"laravel%e3%81%a7%e3%81%ae%e3%82%a4%e3%83%99%e3%83%b3%e3%83%88","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1076","title":{"rendered":"Laravel\u3067\u306e\u30a4\u30d9\u30f3\u30c8"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u306e\u4f5c\u6210<\/h2>\n\n\n\n<h3 id=\"toc1\" class=\"wp-block-heading\">\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306e\u307f\u4f5c\u6210<\/h3>\n\n\n\n<p>\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306f\u901a\u5e38\u3001app\/Events\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4fdd\u5b58\u3059\u308b\u3002<br>\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306e\u307f\u3092\u4f5c\u6210\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# php artisan make:event \u30af\u30e9\u30b9\u540d\n<\/pre><\/div>\n\n\n<h3 id=\"toc2\" class=\"wp-block-heading\">\u30ea\u30b9\u30ca\u30af\u30e9\u30b9\u306e\u307f\u4f5c\u6210<\/h3>\n\n\n\n<p>\u30ea\u30b9\u30ca\u306f\u901a\u5e38\u3001app\/listeners\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4fdd\u5b58\u3059\u308b\u3002<br>\u4f5c\u6210\u3057\u305f\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306b\u7d10\u4ed8\u3051\u3089\u308c\u308b\u30ea\u30b9\u30ca\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# php artisan make:listener \u30ea\u30b9\u30ca\u540d --event=\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u540d\n<\/pre><\/div>\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u3092\u540c\u6642\u306b\u4f5c\u6210\u3057\u3001\u767b\u9332\u307e\u3067\u884c\u3046<\/h3>\n\n\n\n<p>\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u3068\u30ea\u30b9\u30ca\u30af\u30e9\u30b9\u3092\u540c\u6642\u306b\u4f5c\u6210\u3057\u305f\u3044\u5834\u5408\u306f\u3001<br>app\/Providers\/EventServiceProviders\u30af\u30e9\u30b9\u306b\u3066\u6b21\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u53ef\u80fd\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: perl; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Auth\\Events\\Registered;\nuse Illuminate\\Auth\\Listeners\\SendEmailVerificationNotification;\nuse Illuminate\\Foundation\\Support\\Providers\\EventServiceProvider as ServiceProvider;\nuse Illuminate\\Support\\Facades\\Event;\nuse App\\Events\\TestEvent3;        # \u3053\u3053\u3067\u5834\u6240\u3092\u6307\u5b9a\u3002\nuse App\\Listeners\\TestListener3;  # \u3053\u3053\u3067\u5834\u6240\u3092\u6307\u5b9a\u3002\n\nclass EventServiceProvider extends ServiceProvider\n{\n    \/**\n     * The event to listener mappings for the application.\n     *\n     * @var array&lt;class-string, array&lt;int, class-string&gt;&gt;\n     *\/\n    protected $listen = &#x5B;\n        Registered::class =&gt; &#x5B;\n            SendEmailVerificationNotification::class,\n        ],\n        TestEvent2::class =&gt; &#x5B;\n            TestListener2::class,\n        ],\n        TestEvent3::class =&gt; &#x5B;\n            TestListener3::class,\n        ],\n    ];\n\n    \/**\n     * Register any events for your application.\n     *\/\n    public function boot(): void\n    {\n        \/\/\n    }\n\n    \/**\n     * Determine if events and listeners should be automatically discovered.\n     *\/\n    public function shouldDiscoverEvents(): bool\n    {\n        return false;\n    }\n}\n<\/pre><\/div>\n\n\n<p>EventServiceProviders\u30af\u30e9\u30b9\u304c\u4e0a\u8a18\u306e\u72b6\u614b\u3067\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# php artisan event:generate\n<\/pre><\/div>\n\n\n<p>\u3059\u308b\u3068\u3001TestEvent2\u3068TestListener2\u306fapp\/Providers\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4f5c\u6210\u3055\u308c\u3001<br>TestEvent3\u306fapp\/Events\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3001TestListener3\u306fapp\/Listeners\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u305d\u308c\u305e\u308c\u4f5c\u6210\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<h3 id=\"toc4\" class=\"wp-block-heading\">\u767b\u9332\u3055\u308c\u305f\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u3092\u78ba\u8a8d<\/h3>\n\n\n\n<p>\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001\u767b\u9332\u3055\u308c\u305f\u5168\u3066\u306e\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u306e\u30ea\u30b9\u30c8\u3092\u8868\u793a\u3067\u304d\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# php artisan event:list\n<\/pre><\/div>\n\n\n<h2 id=\"toc5\" class=\"wp-block-heading\">\u30a4\u30d9\u30f3\u30c8\u3092\u767a\u706b\u3057\u3066\u307f\u308b<\/h2>\n\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">TestEvent3.php<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace App\\Events;\n\nuse Illuminate\\Broadcasting\\Channel;\nuse Illuminate\\Broadcasting\\InteractsWithSockets;\nuse Illuminate\\Broadcasting\\PresenceChannel;\nuse Illuminate\\Broadcasting\\PrivateChannel;\nuse Illuminate\\Contracts\\Broadcasting\\ShouldBroadcast;\nuse Illuminate\\Foundation\\Events\\Dispatchable;\nuse Illuminate\\Queue\\SerializesModels;\n\nclass TestEvent3\n{\n    use Dispatchable, InteractsWithSockets, SerializesModels;\n\n    private string $tempStr;\n\n    \/**\n     * Create a new event instance.\n     *\/\n    public function __construct(string $tempStr)\n    {\n        $this-&gt;tempStr = $tempStr;\n    }\n\n    public function getStr(): string\n    {\n        return $this-&gt;tempStr;\n    }\n\n    \/**\n     * Get the channels the event should broadcast on.\n     *\n     * @return array&lt;int, \\Illuminate\\Broadcasting\\Channel&gt;\n     *\/\n    public function broadcastOn(): array\n    {\n        return &#x5B;\n            new PrivateChannel(&#039;channel-name&#039;),\n        ];\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"toc7\" class=\"wp-block-heading\">TestListener3.php<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\nnamespace App\\Listeners;\n\nuse App\\Events\\TestEvent3;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Queue\\InteractsWithQueue;\n\nclass TestListener3\n{\n    \/**\n     * Create the event listener.\n     *\/\n    public function __construct()\n    {\n        \/\/\n    }\n\n    \/**\n     * Handle the event.\n     *\/\n    public function handle(TestEvent3 $event): void\n    {\n        var_dump($event-&gt;getStr());\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"toc8\" class=\"wp-block-heading\">\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n\n\n\n<p>\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u3001Tinker\u3067\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n&gt; use App\\Events\\TestEvent3;\n&gt; Event::dispatch(new TestEvent3(&#039;aiueo&#039;));\n<\/pre><\/div>\n\n\n<p>\u3059\u308b\u3068\u3001$strTemp\u306e\u5185\u5bb9\u304cvar_dump\u306b\u3088\u3063\u3066\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30a4\u30d9\u30f3\u30c8\u3068\u30ea\u30b9\u30ca\u306e\u4f5c\u6210 \u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306e\u307f\u4f5c\u6210 \u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306f\u901a\u5e38\u3001app\/Events\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u4fdd\u5b58\u3059\u308b\u3002\u30a4\u30d9\u30f3\u30c8\u30af\u30e9\u30b9\u306e\u307f\u3092\u4f5c\u6210\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002 \u30ea\u30b9\u30ca\u30af\u30e9\u30b9\u306e\u307f\u4f5c\u6210 \u30ea\u30b9\u30ca\u306f\u901a\u5e38\u3001app [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96],"tags":[88,81],"class_list":["post-1076","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-php"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1076"}],"version-history":[{"count":1,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1076\/revisions"}],"predecessor-version":[{"id":1077,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1076\/revisions\/1077"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}