Php Laravel-将变量从一个函数传递到另一个函数

Php Laravel-将变量从一个函数传递到另一个函数,php,laravel,controller,Php,Laravel,Controller,问题很简单,我只想使用另一个函数中的变量,但它没有被识别 任何帮助都将不胜感激 控制器: public function index() { $this->getAllUsers(); return view('home')->with('users', $users); } public function getAllUsers() { $users = User::get(); } 让第二

问题很简单,我只想使用另一个函数中的变量,但它没有被识别

任何帮助都将不胜感激

控制器:

public function index()
    {
        $this->getAllUsers();
        return view('home')->with('users', $users);
    }

    public function getAllUsers()
    {
        $users = User::get();
    }

让第二个函数返回所需的值:

public function index()
    {
      $users=  $this->getAllUsers();
        return view('home')->with('users', $users);
    }

    public function getAllUsers()
    {
        return User::get();
    }

您正在调用getAllUsers方法,但没有将其存储在变量中。此外,getAllUsers方法应返回所有用户

public function index()
{
    //Store it in the $users variable.
    $users = $this->getAllUsers();
    return view('home')->with('users', $users);
}

public function getAllUsers()
{
    return User::get();
}

做2件事,在getAllUsers函数中添加此代码返回$users;并将此代码添加到索引函数$users=$this->getAllUsers;中;。谢谢,成功了!谢谢,成功了!