Laravel 5.4 Undefined从控制器到视图的变量

时间:2017-03-19 17:42:58

标签: laravel laravel-5 laravel-5.4

我最近开始使用Laravel 5.4,我熟悉5.3。我试图将一个变量从我的控制器传递给视图并继续获得一个未定义的变量$ savedStores"。

public function index()
{
    //

    $stores = Store::where('id as user_id')->get();

    return view('home')->with('savedStores', $store);
}

这是我的控制器代码。

在我看来,我有

             @if(count($savedStores)>0)

              @foreach($savesStores as $savedStore)
              <p>{{$savedStore -> name}}</p>

              @endforeach




              @endif

4 个答案:

答案 0 :(得分:1)

return view('home',['savedStores'=>$stores]);
拼写错误chane $ store to $ stores

答案 1 :(得分:0)

实际上是使用会话数据的方法。

您可以将变量传递给视图。

return view('home',['savedStores'=>$stores]);

return view('home',compact('stores')); // as store variable.

答案 2 :(得分:0)

只需更改您的代码:

从:

$stores = Store::where('id as user_id')->get();

为:

$stores = Store::where('id',$some_id_value)->get();

或者您可以通过以下方式获取所有商店记录:

$stores = Store::all();

您正在以正确的方式传递变量以进行查看。

更多细节请点击此处:

https://laravel.com/docs/5.4/views

答案 3 :(得分:0)

试试这个,

public function index()
{

    $stores = Store::select('*','id as user_id')->get();
    dd($stores);//see query result, if you get correct result here, just remove this line
    return view('home')->with('savedStores', $stores );
}