【问题标题】:Laravel event error, to few arguments to functionLaravel 事件错误,函数参数很少
【发布时间】:2019-04-17 08:34:48
【问题描述】:

我正在尝试广播一个事件但没有成功。我找不到问题所在,我没有想法。我得到的错误是。

"Too few arguments to function App\Events\ConversationUpdate::__construct(),
1 passed in /app/Events/ConversationUpdate.php on line 36 and exactly 2 expected"

我想要做的是在创建消息时,将事件广播到聊天并使用新消息实时更新聊天。

这是我的代码:

控制器:

public function newConversationMessage(Thread $conversation, Request $request)
    {
        $user = Auth::user();
        $message = Message::create([
            'thread_id' => $conversation->id,
            'user_id' => $user->id,
            'body' => $request->body,
        ]);
        $message->user; 
        broadcast(new ConversationUpdate($conversation->id, $message));    
        return $message;
    }

事件:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ConversationUpdate implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $conversationId;


    public $message;


    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($conversationId, $message)
    {
        $this->conversationId = $conversationId;
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new ConversationUpdate('conversation-'.$this->conversationId.'-updated');
    }
}

【问题讨论】:

    标签: php laravel events broadcast pusher


    【解决方案1】:

    在函数broadcastOn() 上你应该返回类似这样的东西

    return new PrivateChannel('conversation-'.$this->conversationId.'-updated');
    

    而不是您所在的同一类的另一个实例(错误实际上取决于您在构造函数中传递的单个参数)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2018-04-22
      • 1970-01-01
      • 2020-09-02
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多