Laravel 事件错误,函数参数很少
Laravel event error, to few arguments to function
我正在尝试广播一个活动但没有成功。我找不到问题所在,而且我没有想法。我得到的错误是。
"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');
}
}
在函数 broadcastOn()
上你应该 return 像这样
return new PrivateChannel('conversation-'.$this->conversationId.'-updated');
而不是你所在的同一个 class 的另一个实例(错误实际上取决于你在构造函数中传递的单个参数)
我正在尝试广播一个活动但没有成功。我找不到问题所在,而且我没有想法。我得到的错误是。
"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');
}
}
在函数 broadcastOn()
上你应该 return 像这样
return new PrivateChannel('conversation-'.$this->conversationId.'-updated');
而不是你所在的同一个 class 的另一个实例(错误实际上取决于你在构造函数中传递的单个参数)