doujilou3903 2014-03-16 19:48
浏览 130
已采纳

Laravel在视图中获取Collection对象

in below code i have Eloquent command and that return Collection object and i can not fetch that into view.

$collection = DB::table('contents')
    ->join('categories', function($join)
    {
        $join->on('categories.id', '=', 'contents.category');
    })
    ->where('contents.id', '=', $id)
    ->get();

thats return single Collecton Object and i do not need to use foreach. how to fetch this collection object in view without usign foreach?

i get error after using this :

echo $collection->title;
{{ $collection->title }}

ERROR:

Trying to get property of non-object
  • 写回答

1条回答 默认 最新

  • duanche2007 2014-03-16 20:08
    关注

    What is a single collection object? You must be talking about a single model? Collections are always an array of models.

    Your query should be like:

    $collection = DB::table('contents')
                    ->join('categories', 'contents.category', '=', 'categories.id')
                    ->where('contents.id', '=', $id)
                    ->first();
    
    echo $collection->title;
    

    passing data to the view can be like:

    $data = array(
       'collection' => $collection
    );
    return View::make('collection', $data);
    

    accessing the data from the template:

    {{ $collection->title }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大