视图中的Laravel未定义变量


Laravel Undifined Variable in View

我不知道从我的"欢迎"页面到"个人资料"页面的链接出了什么问题。

上面写着"未定义变量:restaurants (View:/Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"

奇怪的是,我将变量传递给我的视图。

My PagesController is:

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

My View is:

@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} url上。

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

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

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

试试这个,

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

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

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

更改以下行:

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

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