如何在同一控制器中将一些数据从一个函数传递到另一个函数? -Laravel

时间:2017-05-18 03:16:39

标签: php laravel

public function chat($team1,$team2){
    $id1=$team1;
    $id2=$team2;
    return view('chat') ;
}


public function sendMessage(Request $request)
{
     $user = Auth::user();
     $message = $user->messages()->create([
          'message' => $request->input('message'),
          'first_team_id'=>$id1<to call here
          'second_team_id'=>$id2<to call here
     ]);
}

我希望这个来自聊天功能的$ id1和$ id2能够在同一个控制器中发送到sendMessage.This函数中!

路线::得到(&#39; / directchat / {TEAM1} / {TEAM2}&#39; [&#39;如&#39; = GT;&#39; chat.start&#39 ;, &#39;使用&#39; = GT;&#39; ChatsController @聊天&#39;]);

路由::帖子(&#39;消息&#39;,&#39; ChatsController @ sendMessage&#39;);

1 个答案:

答案 0 :(得分:0)

chat.blade.php:

<div>
    Your chat view code
    <form action="/messages" method="POST">
          <input type="hidden" name="team1" value="{{ $team1 }}">
          <input type="hidden" name="team2" value="{{ $team2 }}">
          <input type="submit" value="Send Message Link">
    </form>
</div>

你的控制器看起来像

public function chat($team1,$team2){
    return view('chat' ,["team1"=>$team1,"team2"=>$team2]) ;
}


public function sendMessage(Request $request)
{
     $user = Auth::user();
     $message = $user->messages()->create([
          'message' => $request->input('message'),
          'first_team_id'=> request('team1'),
          'second_team_id'=> request('team1')
     ]);
}