Php 未定义变量Laravel Foreach

Php 未定义变量Laravel Foreach,php,laravel,blade,Php,Laravel,Blade,我想获取一些数据,但它说它不知道变量: 我得到的错误是: ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 51: Undefined variable: albums (View: /var/www/clients/client2/web2/web/resources/views/album.blade.php) 这是我的代码: Album.blade.php @foreach($al

我想获取一些数据,但它说它不知道变量:

我得到的错误是:

    ErrorException in dbda158712a631f22ffd888cd244c74e60f3a433.php line 51:
Undefined variable: albums (View:        /var/www/clients/client2/web2/web/resources/views/album.blade.php)
这是我的代码:
Album.blade.php

@foreach($albums as $others)
    <option value="{{$others->id}}">{{$others->name}}</option>
@endforeach

您正在通过view album.blade.php传递album变量,该变量是单个对象,而不是对象数组,因此不能在循环中迭代

我认为你犯了一个错误

您希望在index.blade.php中执行foreach,因为这里传递的是albums变量


您需要在getList函数中返回view album.blade.php

通过view album.blade.php传递album变量,该变量是单个对象,而不是对象数组,因此不能在循环中迭代

我认为你犯了一个错误

您希望在index.blade.php中执行foreach,因为这里传递的是albums变量

您需要在getList函数中返回view album.blade.php

试试这段代码

 public function getAlbum($id)
 {
      $albums=albums::with('Photos')->get();

       $album=albums::with('Photos')->find($id);

       return view('album',compact('albums','album'));

 }
试试这个代码

 public function getAlbum($id)
 {
      $albums=albums::with('Photos')->get();

       $album=albums::with('Photos')->find($id);

       return view('album',compact('albums','album'));

 }

使用
@foreach($album as$others)
@CharlotteDunois,它会给我以下错误:尝试获取非对象的属性发布完整的错误消息返回视图如何('album',['album'=>$album]):DBDA15871A631F22FFD888CD244C74E60F3A433.php行52中的错误异常:尝试获取非对象的属性(视图:/var/www/clients/client2/web2/web/resources/views/album.blade.php)使用
@foreach($album as$others)
@CharlotteDunois,这给了我以下错误:尝试获取非object属性发布完整的错误消息返回视图('album',['album'=>$album])怎么样:DBDA15871A631F22FFD888CD244C74E60F3A433.php行52中的错误异常:尝试获取非对象的属性(视图:/var/www/clients/client2/web2/web/resources/views/album.blade.php)但是我也需要索引中的getList,有没有一种方法可以在多个视图中返回它?你想在两个刀片文件中都返回list?如果是,那么你不能在一个函数中返回两个视图,但是你可以在一个视图中传递多个变量,但是我也需要索引中的getList,有没有一种方法可以在多个视图中返回它?你想在两个刀片文件中都返回liste?如果是,则不能在一个函数中返回两个视图,但可以在一个视图中传递多个变量