如何在控制器中保存字符串变量并将其传递到Laravel的视图中


How to save variable in string in controller and pass it into view in Laravel

我有一个方法,我从db中抓取所有数据并将其传递给视图。没问题,然后在视图中,我可以选择$location->ip(用于从特定用户获取位置)但我试图在控制器中获取字符串,把它放在这个变量

$location = GeoIP::getLocation($getIp);

,然后将该变量传递给视图。我该怎么做呢?

您可以使用View::share()使您的变量在所有视图文件中可用。只需这样做:

View::share('location ', GeoIP::getLocation($getIp));

现在您可以通过$location变量在任何视图中访问location变量。