Laravel Unndifined Variable in View

时间:2016-08-26 10:40:15

标签: php laravel blade

我无法弄清楚我的"欢迎"的链接出了什么问题?页面到"个人资料"页。

它说"未定义的变量:餐馆(查看:/Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"

奇怪的丁字裤是我将变量传递给我的视图。

我的PagesController是:

public function welcome() {
  $restaurants = User::orderByRaw('RAND()')->take(3)->get();
  return view('welcome')->withRestaurants($restaurants);
}

我的观点是:

@foreach($restaurants as $key => $restaurant)
            <div class="col-md-4">
                <div class="card">
                    <div class="image">
                        <img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
                        <div class="filter filter-white">
                            <a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
                                <i class="fa fa-heart"></i> View Restaurant
                            </a>
                        </div>
                    </div>
                </div>
            </div>
          @endforeach

将餐厅变量正确传递到我的欢迎视图,但是当我点击该链接时,/ profile / {id}网址上会出现错误。

3 个答案:

答案 0 :(得分:0)

更改以下行:

return view('welcome')->withRestaurants($restaurants);   // there is nothing like withRestaurants in laravel

return view('welcome', array('restaurants' => $restaurants));

再试一次。

答案 1 :(得分:0)

更改return view('welcome')->withRestaurants($restaurants);

return view('welcome')->with('restaurants', $restaurants);

OR

 return view('welcome', compact('restaurants');

答案 2 :(得分:0)

试试这个,

return view('welcome')->with("restaurants",$restaurants);

OR

return view('welcome',["restaurants" => $restaurants]);

检查laravel文档:https://laravel.com/docs/master/views#passing-data-to-views