最后一个元素移动到第一个元素的Laravel
我正在尝试在laravel 5中构建简单的CMS。我的问题是: 我不想把测试的新主题移到第一页,第一个值。因为我在PhpMyAdmin中有最后一个元素移动到第一个元素的Laravel,laravel,foreach,Laravel,Foreach,我正在尝试在laravel 5中构建简单的CMS。我的问题是: 我不想把测试的新主题移到第一页,第一个值。因为我在PhpMyAdmin中有是新主题。所以我不想检查is post isis\u new\u topic==1,然后将其移动到第一个foreach值 查看主题控制器: <?php namespace App\Http\Controllers; use DB; use View; class viewTopic extends Controller { public
是新主题。所以我不想检查is post isis\u new\u topic==1
,然后将其移动到第一个foreach值
查看主题
控制器:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewTopic extends Controller
{
public function showTopic($tname, $tid)
{
$topics = DB::table('topics')
->where('id', $tid)
->where('seo_title', $tname)
->first();
$posts = DB::table('posts')
->where('topic_id', $tid)
->join('users', 'users.id', '=', 'posts.author_id')
->select()
->paginate(3);
return View::make('posts', compact('topics', 'posts'));
}
}
提前感谢:)我假设是\u new\u topic
列中的值是1表示新,0表示旧。您必须按该列排序desc
,如下面的代码所示:
$posts = DB::table('posts')
->where('topic_id', $tid)
->join('users', 'users.id', '=', 'posts.author_id')
->orderBy('is_new_topic', 'desc');
->select()
->paginate(3);
按是新的主题描述,点菜不是更简单吗?@Alexartan IDK我是新手,怎么做?哦,xd,很有效,非常感谢;)您现在可以发布自己的答案并接受它;)一般来说,最好按创建日期排序。这样做的工作量更少,在做进一步的更改时(例如,删除最新评论时会发生什么情况),您也不需要关心它。
$posts = DB::table('posts')
->where('topic_id', $tid)
->join('users', 'users.id', '=', 'posts.author_id')
->orderBy('is_new_topic', 'desc');
->select()
->paginate(3);