質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Slack

Slackは、Tiny Speckという企業からリリースされたコミュニケーションツールです。GoogleDriveやGitHubなど、さまざまな外部サービスと連携することができます。

Q&A

解決済

1回答

1547閲覧

laravel 7.3.0でのslack-notification-channelのSlackMessageインスタンスの使い方について

DeepRoastBeans

総合スコア79

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Slack

Slackは、Tiny Speckという企業からリリースされたコミュニケーションツールです。GoogleDriveやGitHubなど、さまざまな外部サービスと連携することができます。

0グッド

0クリップ

投稿2020/03/27 14:14

Laravel7.3.0で、slack-notification-channelをインストールしたところ、SlackMessageやSlackAttachmentが、Illuminate\Notifications\Messagesとは別のところに作られているように見えます。

// Laravel version
$ php artisan --version
Laravel Framework 7.3.0

// slack-notification-channelインストール
$ php composer.phar require laravel/slack-notification-channel

イメージ説明

// Illuminate/Notifications/MessagesにSlackMessageがありません。
$ ls -la vendor/laravel/framework/src/Illuminate/Notifications/Messages
合計 28
drwxrwxr-x 2 vagrant vagrant 109 3月 25 00:17 .
drwxrwxr-x 7 vagrant vagrant 4096 3月 25 00:17 ..
-rw-rw-r-- 1 vagrant vagrant 622 3月 25 00:17 BroadcastMessage.php
-rw-rw-r-- 1 vagrant vagrant 405 3月 25 00:17 DatabaseMessage.php
-rw-rw-r-- 1 vagrant vagrant 6960 3月 25 00:17 MailMessage.php
-rw-rw-r-- 1 vagrant vagrant 4895 3月 25 00:17 SimpleMessage.php

SlackMessageインスタンスを使おうとしましたが、上手く行きません。

php

1namespace App\Notifications; 2 3use Illuminate\Bus\Queueable; 4use Illuminate\Contracts\Queue\ShouldQueue; 5use Illuminate\Notifications\Messages\MailMessage; 6use Illuminate\Notifications\Messages\SlackMessage; 7use Illuminate\Notifications\Messages\SlackAttachment; 8use Illuminate\Notifications\Notification; 9 10class Slack extends Notification 11{ 12 use Queueable; 13 14 protected $content; 15 protected $channel; 16 protected $name; 17 protected $icon; 18 19 /** 20 * Create a new notification instance. 21 * 22 * @return void 23 */ 24 public function __construct($message) 25 { 26 // 27 $this->channel = env('SLACK_CHANNEL'); 28 $this->name = env('SLACK_NAME'); 29 $this->icon = env('SLACK_ICON'); 30 $this->content = $message; 31 } 32 33 /** 34 * Get the notification's delivery channels. 35 * 36 * @param mixed $notifiable 37 * @return array 38 */ 39 public function via($notifiable) 40 { 41 return ['slack']; 42 } 43 44 /** 45 * Get the array representation of the notification. 46 * 47 * @param mixed $notifiable 48 * @return array 49 */ 50 public function toSlack($notifiable) 51 { 52 return (new SlackMessage) 53 ->from($this->name) 54 ->image($this->icon) 55 ->to($this->channel) 56 ->content($this->content); 57 } 58 59}

$ php artisan slack:send 'Hello world!! From Laravel Command'

Illuminate\Contracts\Container\BindingResolutionException

Target [App\Repositories\Slack\SlackRepositoryInterface] is not instantiable.

laravel7.xの公式ドキュメントを見ると、 Illuminate\Notifications\Messages\MailMessageを使うように書いていますが、どのように使ったらいいのでしょうか? やり方が間違っていたら教えていただけると幸いです。
https://readouble.com/laravel/7.x/ja/notifications#slack-notifications

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

元々laravel/frameworkに含まれてたのをslack-notification-channelに分離しただけなのでインストール場所は変わっても名前空間は同じ。使い方も同じ。
https://github.com/laravel/slack-notification-channel/blob/2.0/src/Messages/SlackMessage.php

エラーはこれなんだから原因は自分で作ってるSlackRepositoryInterface
slack-notification-channel側は何も関係ない。

Target [App\Repositories\Slack\SlackRepositoryInterface] is not instantiable.

それとここのenv()も使えない。

$this->channel = env('SLACK_CHANNEL'); $this->name = env('SLACK_NAME'); $this->icon = env('SLACK_ICON');

開発中しか正常に動かない。
env()使うように教えてる人は本番環境で運用したことのない初心者。
なぜかLaravelはそんな人が自信満々に教えていて迷惑している。

投稿2020/03/27 22:53

kawax

総合スコア10377

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DeepRoastBeans

2020/03/27 23:46

仕組みについて理解いたしました。envも承知いたしました。ご丁寧に大変ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問