2013-04-28 49 views
0

我试图建立一个小部件设计系统,用户可以提交小部件的标题和html.Now当我尝试在视图中查询Widget模型并将数据传递到@foreach循环时,我得到的错误为@foreach无法遍历由Widget::all()返回的查询集。如何在我的网页上显示来自Widget模型的所有数据?如何从Laravel的视图中检索所有模型数据?

Btw我的Widget模型只有两个字段(即标题和HTML)。

编辑:以下是我所得到的回报的var_dump当我做Widget::all()

array(2) { [0]=> object(Widget)#42 (5) { ["attributes"]=> array(3) { ["id"]=> string(1) "1" ["title"]=> string(24) "Join Demo classes today!" ["html"]=> string(47) " 
This is just the great demo of widgets. 
" } ["original"]=> array(3) { ["id"]=> string(1) "1" ["title"]=> string(24) "Join Demo classes today!" ["html"]=> string(47) " 
This is just the great demo of widgets. 
" } ["relationships"]=> array(0) { } ["exists"]=> bool(true) ["includes"]=> array(0) { } } [1]=> object(Widget)#45 (5) { ["attributes"]=> array(3) { ["id"]=> string(1) "2" ["title"]=> string(12) "About Google" ["html"]=> string(66) "Google is the best site in the world." } ["original"]=> array(3) { ["id"]=> string(1) "2" ["title"]=> string(12) "About Google" ["html"]=> string(66) "Google is the best site in the world." } ["relationships"]=> array(0) { } ["exists"]=> bool(true) ["includes"]=> array(0) { } } } 
+0

你是如何循环,它看起来像一个对象数组。必须是循环问题。 – 2013-04-28 09:04:21

+0

也许你可以展示你的行动代码和你的视图代码。没有理由不能迭代Widget :: all()的结果 – 2013-04-28 09:09:50

回答

2

很难无需任何代码来解决你的问题。这是我会做:

控制器:

$widgets = Widget::all(); 
View::make('html.widgets')->with('widgets', $widgets); 

视图(刀片):

@foreach($widgets as $widget) 
    {{ $widget->title }} 
    {{ $widget->html }} 
@endforeach 

在你提的查询窗口小部件在视图中的问题。由于这显然违背了MVC的原则,但是证明了laravel的灵活性,我还会给你一个片段,说明如何在没有控制器的情况下做到这一点。我做建议是:

@foreach(Widget::all() as $widget) 
    {{ $widget->title }} 
    {{ $widget->html }} 
@endforeach 
相关问题