Laravel 4的all()方法最近开始返回雄辩收藏 - 为什么? [英] Laravel 4 's all() method started to return Eloquent collection lately - why?
问题描述
这是我的控制器:
return View :: make('home.listings')
- > ; with('listing',Listing :: all());
以前,我检查过这样的意见:(并且它在我所有的项目上工作) p>
@if(!empty($ listings))
//使用foreach并显示列表
@else
< h3>未找到列表。< / h3>
@endif
这是我的上市模式。
类列表扩展Eloquent {
public static $ key ='id';
protected $ table ='ilanlar';
public $ timestamps = false;
}
现在,它在数据库中有条目时起作用。然而,当没有,它不会得到其他块。因为:
dd(!empty($ listing))//输出:(boolean)true
当我输出列表变量时:
dd($ listing)// outputs:
object(Illuminate\Database\Eloquent\Collection)$ 229 $ b protected'items'=>
数组(size = 0)
empty
没有达到其他的阻止,因为Eloquent返回和对象,并将其转换为true。
问题是:我不知道为什么它返回一个雄辩的收藏。以前没有。我是忽略某些东西吗?
Collection对象有一个 isEmpty()
方法,所以你可以使用that:
@if(!$ listing-> isEmpty())
//使用foreach并显示列表
@else
< h3>未找到列表。< / h3>
@endif
或者,您可以将所有内容转换为数组:
return View :: make('home.listings')
- > with('listing',Listing :: all() - >指定者());
在视图中使用数组:
$ b $
$ {$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ name']}}< / li> <! - 注意$列表被视为数组 - >
@endforeach
@else
< h3>未找到列表。< / h3>
@endif
This is my controller:
return View::make('home.listings')
->with('listings', Listing::all());
Previously, I checked it on views like this: (and it worked on all my projects)
@if(!empty($listings))
//use foreach and show listings
@else
<h3>No listing is found.</h3>
@endif
This is my Listing model.
class Listing extends Eloquent {
public static $key = 'id';
protected $table = 'ilanlar';
public $timestamps = false;
}
Right now, it works when there is an entry in database. However, when there is not, it doesn't get to the else block. Because:
dd(!empty($listings)) //output: (boolean) true
And, when I output listings variable:
dd($listings) //outputs:
object(Illuminate\Database\Eloquent\Collection)[229]
protected 'items' =>
array (size=0)
empty
This is the reason why it doesn't reach else block, because Eloquent returns and object and it casts to true.
The problem is: I don't know why it returns an Eloquent collection. Previously it didn't. Am I overlooking something?
The Collection object has a isEmpty()
method, so you could use that:
@if(! $listings->isEmpty())
//use foreach and show listings
@else
<h3>No listing is found.</h3>
@endif
Alternatively, you can convert everything to an Array:
return View::make('home.listings')
->with('listings', Listing::all()->toArray());
And use everything as an array within your views:
@if(!empty($listings))
@foreach(listings as $listing):
<li>{{ $listing['name'] }}</li> <!-- Note $listing is treated as an array -->
@endforeach
@else
<h3>No listing is found.</h3>
@endif
这篇关于Laravel 4的all()方法最近开始返回雄辩收藏 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!