Laravel错误必须是..聊天应用程序的实例

时间:2018-02-18 13:39:16

标签: php laravel vue.js

我有问题。我使用事件:https://github.com/musonza/chat/blob/master/src/Messages/MessageWasSent.php

在我的控制器中:

public function sendMessage(Conversation $conversation, Request $request) {
    $v = Validator::make($request->all(), [
         'message' => 'required|string|max:4000',
    ]);

    $user = User::find(Auth::id());
    $conv = Chat::conversation($conversation->id);

    Chat::message($request->message)
        ->from($user)
        ->to($conv)
        ->send();

    event(new MessageWasSent($request->message));
}

我收到错误:

Type error: Argument 1 passed to Musonza\Chat\Messages\MessageWasSent::__construct() must be an instance of Musonza\Chat\Messages\Message, string given..

在我的app.js中:

send(){
        if(this.message.length != 0 && this.message.length <= 4000) {
            this.chat.message.push(this.message);
            this.chat.user.push(this.user);
            this.chat.time.push(this.getTime());
            axios.post('/sendMessage/' + this.convId, {
                message: this.message,
              })
              .then(response => {
                console.log(response);
                this.message = '';
              })
              .catch(error => {
                console.log(error);
              });
        }
    }

路线:

    Route::post('sendMessage/{conversation}', 'Chat\ChatController@sendMessage')->name('chatsend');

我如何解决此错误?

1 个答案:

答案 0 :(得分:0)

$message = Chat::message($request->message)
        ->from($user)
        ->to($conv)
        ->send();

event(new MessageWasSent($message));

由于错误说事件构造函数需要Musonza\Chat\Messages\Message的实例而不是字符串