Php 视图中的Laravel fetch集合对象
在下面的代码中,我有一个雄辩的命令,返回集合对象,但我无法将其提取到视图中Php 视图中的Laravel fetch集合对象,php,laravel,laravel-4,Php,Laravel,Laravel 4,在下面的代码中,我有一个雄辩的命令,返回集合对象,但我无法将其提取到视图中 $collection = DB::table('contents') ->join('categories', function($join) { $join->on('categories.id', '=', 'contents.category'); }) ->where('contents.id', '=', $id) ->get()
$collection = DB::table('contents')
->join('categories', function($join)
{
$join->on('categories.id', '=', 'contents.category');
})
->where('contents.id', '=', $id)
->get();
这将返回单个Collecton对象,我不需要使用foreach
。
如何在不使用usignforeach
的情况下获取视图中的此集合对象
使用此选项后出现错误:
echo $collection->title;
{{ $collection->title }}
错误:
Trying to get property of non-object
什么是单个集合对象?你一定是在说一个模型吧?集合始终是一组模型 您的查询应该如下所示:
$collection = DB::table('contents')
->join('categories', 'contents.category', '=', 'categories.id')
->where('contents.id', '=', $id)
->first();
echo $collection->title;
将数据传递到视图可以类似于:
$data = array(
'collection' => $collection
);
return View::make('collection', $data);
从模板访问数据:
{{ $collection->title }}
在视图上
{$collection->title}
如果您不想使用foreach,那么{{var_dump($collection)}
@majimboo我得到了这个错误:试图获取{$collection->title}
的非对象属性,因为这是一个对象数组。看看我的答案。