topic->user->name报错
html
<div class="panel-body remove-padding-horizontal">
<ul class="list-group row thread-list">
<?php foreach($topics as $topic) :?>
<li class="list-group-item media " style="margin-top: 0px;">
<span class="badge badge-reply-count"> </span>
<div class="avatar pull-left">
<a href="post.html">
<img class="media-object img-thumbnail avatar-48" alt="imooc" src="__IMG__/avatar_small.jpg"/>
</a>
</div>
<div class="infos">
<div class="media-heading">
<i class="fa fa-thumb-tack text-danger"></i>
<a href="<?=url('topic/detail');?>?id=<?=$topic->id;?>" title="<?=$topic->title;?>">
<?=$topic->title;?>
</a>
</div>
<div class="media-body meta">
<a>
<span class="fa fa-thumbs-o-up"> <?=$topic->praises_count;?> </span>
</a>
<span> • </span>
<?php $categorys = getCategoryNames($topic->category_id);?>
<span><?=$categorys[0];?></span>
<span> / </span>
<span><?=$categorys[1];?></span>
<span> • </span>
<a><?=$topic->user->name;?></a>
<span> • </span>
<span class="timeago" data-toggle="tooltip" data-placement="top" title="<?=date('Y-m-d H:i:s', $topic->created_at);?>">
<?=date('Y-m-d H:i:s', $topic->created_at);?>
</span>
</div>
</div>
</li>
<?php endforeach;?>
</ul>
</div>
model
public static function getTopics()
{
return self::withCount(['praises'])->select();
}
controller
$topics = TopicModel::getTopics();
$this->assign([
'user' => session('user'),
'topics' => $topics,
'page' => $pageInfo['page'],
'showPages' => $pageInfo['showPages'],
'pageNum' => $pageInfo['pageNum'],
]);
echo $this->fetch('index');
}
正在回答 回答被采纳积分+1
你好,通过测试建议参考如下:
model\topic.php下
public static function getTopics() { return self::withCount(['praises'])->select(); }
改为:
public static function getTopics($page, $limitNum, $sortInfo) { $sortInfo['field'] = $sortInfo['field'] ? $sortInfo['field'] : 'created_at'; $sortInfo['order'] = $sortInfo['order'] ? $sortInfo['order'] : 'desc'; return Topic::withCount(['replies', 'praises']) ->where(['is_delete' => 0]) ->page($page, $limitNum) ->order($sortInfo['field'], $sortInfo['order']) ->select(); }
controller\topic.php下
index()方法改为:
public function index(){ $getData = input('get.'); $page = isset($getData['page']) ? $getData['page'] : 1; $field = isset($getData['field']) ? $getData['field'] : ''; $order = isset($getData['order']) ? $getData['order'] : ''; $sortInfo = ['field' => $field, 'order' => $order]; $pageInfo = TopicModel::getPageInfo($page, config('limitNum')); $cacheName = 'index'.$page.$field.$order; if (cache($cacheName)) { $topics = cache($cacheName); } else { $topics = TopicModel::getTopics($pageInfo['page'], config('limitNum'), $sortInfo); cache($cacheName, $topics, 20); } $this->assign([ 'topics' => $topics, 'user' => session('user'), 'page' => $pageInfo['page'], 'pageNum' => $pageInfo['pageNum'], 'showPages' => $pageInfo['showPages'], 'hotTags' => TopicTagModel::getHotTags(config('hotTagNum')), ]); echo $this->fetch('index'); }
如果解决您的问题请采纳,祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星