未定义的变量 Laravel Foreach
Undefined variable Laravel Foreach
我想 foreach 一些数据,但它说它不知道变量:
我得到的错误:
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
我的相册控制器功能
public function getAlbum($id)
{
$album = Album::with('Photos')->find($id);
return View::make('album')
->with('album',$album);
}
public function getList()
{
$albums = Album::with('Photos')->get();
return View::make('index')
->with('albums',$albums);
}
您正在通过视图 Album.blade.php 传递相册变量,它是单个对象,而不是对象数组,因此您不能在循环中迭代。
我认为你做错了。
您想在 index.blade.php 中执行 foreach,因为您要在此处传递 albums 变量。
或
您需要 return 在您的 getList 函数中查看 album.blade.php。
试试这个代码。
public function getAlbum($id)
{
$albums=albums::with('Photos')->get();
$album=albums::with('Photos')->find($id);
return view('album',compact('albums','album'));
}
我想 foreach 一些数据,但它说它不知道变量:
我得到的错误:
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
我的相册控制器功能
public function getAlbum($id)
{
$album = Album::with('Photos')->find($id);
return View::make('album')
->with('album',$album);
}
public function getList()
{
$albums = Album::with('Photos')->get();
return View::make('index')
->with('albums',$albums);
}
您正在通过视图 Album.blade.php 传递相册变量,它是单个对象,而不是对象数组,因此您不能在循环中迭代。
我认为你做错了。
您想在 index.blade.php 中执行 foreach,因为您要在此处传递 albums 变量。
或
您需要 return 在您的 getList 函数中查看 album.blade.php。
试试这个代码。
public function getAlbum($id)
{
$albums=albums::with('Photos')->get();
$album=albums::with('Photos')->find($id);
return view('album',compact('albums','album'));
}