简体   繁体   English

我想在同一控制器的不同函数中传递变量-Laravel

[英]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; } 
  • Declare this class parameter in your __construct method 在__construct方法中声明此类参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一控制器laravel的不同功能中使用变量 - Using variable in a different function of the same controller laravel Laravel Controller-在函数之间传递变量 - Laravel Controller - Pass a variable from function to function Laravel:如何在同一控制器中将变量从一个函数传递给来自Ajax调用的另一个函数 - Laravel : how to pass variable from one function to other coming from ajax call in same controller 如何在同一个 controller (laravel) 中的函数之间传递变量? - how can i pass a variable between functions in the same controller (laravel)? 将参数从一个函数传递到同一控制器 Laravel 中的另一个函数 - Pass parameter from one function to other function in the same Controller Laravel 在laravel控制器中如何将变量从一个函数传递到另一函数 - in laravel controller how to pass variable from one function to other function 我想从控制器向Laravel的Trait发送变量 - I want to send a variable from a Controller to a Trait on Laravel 将变量从一个函数传递到同一控制器中的另一个函数(laravel 5) - Passing variable from one function to another function in same controller (laravel 5) Laravel将变量从控制器中的1个函数传递给另一个函数 - Laravel pass variable from 1 function in controller to another one Laravel路由传递变量到控制器不一样 - Laravel route pass variable to controller not same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM