[英]Laravel - Passing a variable from a function to another function
The question is pretty simple, all I want to do is to use a variable from another function, but it doesn't get recognized.问题很简单,我想做的就是使用另一个 function 的变量,但它没有被识别。
Any help would be appreciated.任何帮助,将不胜感激。
Controller: Controller:
public function index()
{
$this->getAllUsers();
return view('home')->with('users', $users);
}
public function getAllUsers()
{
$users = User::get();
}
let the second function return the values you want:让第二个 function 返回您想要的值:
public function index()
{
$users= $this->getAllUsers();
return view('home')->with('users', $users);
}
public function getAllUsers()
{
return User::get();
}
You're calling the getAllUsers method, but you're not storing it in a variable.您正在调用 getAllUsers 方法,但没有将其存储在变量中。 Also the getAllUsers method should
return
all users. getAllUsers 方法也应该
return
所有用户。
public function index()
{
//Store it in the $users variable.
$users = $this->getAllUsers();
return view('home')->with('users', $users);
}
public function getAllUsers()
{
return User::get();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.