dongtaihui5131
2018-11-26 15:42
浏览 228
已采纳

如何使用Laravel将变量从一个控制器传递到另一个控制器

To begin with the user selects a value from the dropdown list which stores the selected value in a variable called $SetNumber which is located in ControllerOne. Once the user selects from the dropdown as you cans see the form action is /Update which is used to retrieve and redirect the data to ControllerTwo

<form method="POST" action="/Update">
    @csrf 
    <select name="Match" id="Match" onchange="this.form.submit()">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</form> 

ControllerOne contains an empty variable, which defines the variable $SetNumber. ControllerOne:

class ControllerOne extends Controller
{
    public function index()
    {
        $SetNumber = "";
    }


    public function update(Request $request)
    {
         $SetNumber = $request->Match;

         return redirect()->action('ControllerTwo@index', ['SetNumber' => $SetNumber]);
    }   
}



class ControllerTwo extends Controller
{
    public function index()
    {
        echo $SetNumber;

        return view('pages.viewpage')->with('SetNumber', $SetNumber);
    }
}

The problem is that when I try to echo $SetNumber within my controller and return it to my view , prompts me with this error:

Undefined variable: SetNumber.

how could I do this better?

i am new to laravel and web dev.

图片转代码服务由CSDN问答提供 功能建议

首先,用户从下拉列表中选择一个值,该值将所选值存储在名为的变量中 $ SetNumber ,位于 ControllerOne 中。 一旦用户从下拉列表中选择,您就可以看到表单操作是 / Update ,用于检索数据并将数据重定向到 ControllerTwo

 &lt; form method =“POST”action =“/ Update”&gt; 
 @csrf 
&lt; select name =“Match”id =“Match  “onchange =”this.form.submit()“&gt; 
&lt; option value =”1“&gt; 1&lt; / option&gt; 
&lt; option value =”2“&gt; 2&lt; / option&gt; 
  &lt; option value =“3”&gt; 3&lt; / option&gt; 
&lt; / select&gt; 
&lt; / form&gt;  
   
 
 

ControllerOne 包含 empty 变量,该变量定义变量 $ SetNumber ControllerOne

 类ControllerOne扩展Controller 
 {
公共函数索引()
 {
 $ SetNumber =“”; 
  } 
 
 
公共函数更新(请求$请求)
 {
 $ SetNumber = $ request-&gt;匹配; 
 
返回redirect() - &gt;动作('ControllerTwo @ index',[  'SetNumber'=&gt; $ SetNumber]); 
} 
} 
 
 
 
class ControllerTwo扩展Controller 
 {
公共函数索引()
 {
 echo $ SetNumber; 
 \  n返回视图('pages.viewpage') - &gt; with('SetNumber',$ SetNumber); 
} 
} 
   
 
 

问题在于 当我尝试在控制器中回显 $ SetNumber 并将其返回到我的视图时,提示我出现此错误:

未定义的变量:SetNumber 。

我怎样才能做得更好?

我是laravel和web dev的新手。 \ n

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题