[英]I want to pass variable in different function from same controller - Laravel
public function chat($id,$team1,$team2){
$relation=Crelation::where('match_id',$id)->where('first_team_id',$team1)->where('second_team_id',$team2)->first();
if($relation == null){
$data=[
'match_id'=>$id,
'first_team_id'=>$team1,
'second_team_id'=>$team2
];
$rel= Crelation::create($data);
$whatRelation=$rel->id;
$this->sendMessage($whatRelation);
}else{
$whatRelation=$relation->id;
$this->sendMessage($whatRelation);
}
return view('chat',compact('whatRelation'));
}
public function sendMessage(Request $request,$whatRelation)
{
$id=(int)$whatRelation;
$user = Auth::user();
$message = $user->messages()->create([
'message' => $request->input('message'),
'crelation_id'=>$id
]);
broadcast(new MessageSent($user, $message))->toOthers();
return ['status' => 'Message Sent!'];
}
I get this error : 我收到此错误:
Argument 1 passed to App\\Http\\Controllers\\ChatsController::sendMessage() must be an instance of Illuminate\\Http\\Request, integer given, called in C:\\xampp\\htdocs\\ScrimWithMe\\app\\Http\\Controllers\\ChatsController.php on line 77 and defined
传递给App \\ Http \\ Controllers \\ ChatsController :: sendMessage()的参数1必须是Illuminate \\ Http \\ Request的实例,给定整数,在C:\\ xampp \\ htdocs \\ ScrimWithMe \\ app \\ Http \\ Controllers \\ ChatsController.php中调用在第77行并定义
you have two parameters needed for the sendMessage
function .. but you're just passing one parameter .. 你有两个
sendMessage
函数需要的参数..但是你只是传递了一个参数..
what you can do is add another parameter in your chat
function chat like 您可以做的是在
chat
功能chat中添加另一个参数,例如
public function chat(Request $request, $id,$team1,$team2){
....
$this->sendMessage($request,$whatRelation);
}
then add a parameter in said function and that should do it .. 然后在所述函数中添加一个参数,应该这样做..
@Demonyowh explain all that why you get this error @Demonyowh解释所有为什么出现此错误的原因
I think it's factory design pattern there are two ways to solve your problem 我认为这是工厂设计模式,有两种方法可以解决您的问题
If your method not used on any other place then remove first parameter and declare class on next line 如果您的方法未在其他任何地方使用,则删除第一个参数并在下一行声明类
METHOD(){ $request = new Request(); // your code; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.