企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 微博数据流 上一节中,我们虽然创建了发布微博的功能,可是发布之后并不能实时的看到自己发布的内容,也无法在首页看到大家发布的内容,所以本节将会在首页添加微博动态的功能。 ## 读取微博表 要获取整个微博的内容,非常简单的,我们只需要读取微博表并倒序即可。 `application\welcome\controller\Index.php` ~~~php <?php namespace app\welcome\controller; use think\Controller; use app\User\model\Post; class Index extends Controller { public function home() { $this->assign([ 'posts' => Post::with('user')->order('created_at', 'desc')->paginate(20) ]); return $this->fetch(); } ... } ~~~ 编辑前端页面 `resources\views\welcome\index\home.blade.php` ~~~html ... <div> <div class="list-group list-group-flush"> @foreach ($posts as $post) <div class="list-group-item"> <div> {{ $post->user->name }} -- {{ $post->created_at }} </div> <div> {{ $post->content }} </div> </div> @endforeach </div> {!! $posts !!} </div> @stop ... ~~~ 再次访问首页,即可看到所有微博已经呈现出来。