简体   繁体   English

Laravel - 将变量从 function 传递到另一个 function

[英]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.

相关问题 将变量从一个函数传递到同一控制器中的另一个函数(laravel 5) - Passing variable from one function to another function in same controller (laravel 5) 将变量从一个函数传递到另一个函数 - Passing variable from one function to another function 将变量值从一个函数传递到另一个函数 - Passing variable value from one function to another Laravel-将变量从一个函数传递给另一个函数 - Laravel - Pass variable from a function to another 将变量从一个ajax函数传递到另一个ajax函数 - passing variable from one ajax function to another ajax function 将变量从一个函数传递到同一控制器中的另一个函数 - Passing variable from one function to another function in same controller 在Codeigniter中将uri分段变量从一个函数传递给另一个函数 - Passing uri segment variable from one function to another in Codeigniter Codeigniter:将post变量从一个函数传递到控制器中的另一个函数 - Codeigniter: passing a post variable from one function to another in a controller 无法在PHP中将变量从一个函数传递给另一个函数? - Trouble passing a variable from one function to another in PHP? 通过AJAX调用以自动完成方式将变量值从一个函数传递给另一个函数 - Passing variable value from one function to another in autocomplete with AJAX call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM