<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 顯示微博 本節將為每個人顯示自己發布的微博。 ## 關聯查詢 在上一節中,我們定義了 ~~~php // 正向關聯 Post 模型 public function posts() { return $this->hasMany('Post', 'user_id'); } ~~~ 非常簡單的,在控制器中只需要多加一句 `with('posts')` 即可展示該用戶的所有微博 `application\user\controller\Auth.php` ~~~php public function read($id) { $user = User::with('posts')->find($id); return $user; } ~~~ 接著,訪問 http://thinkphp.test/user/auth/read/id/1.html ,可以看到一行 `"posts":[]` 這是因為現在 `posts` 表中還未有數據,我們需要為用戶填充一些假數據。 命令行鍵入 `php think seed:create Posts` `database\seeds\Posts.php` ~~~php <?php use think\migration\Seeder; class Posts extends Seeder { public function run() { $faker = Faker\Factory::create(); $data = []; for ($i = 0; $i < 200; $i++) { $user_id = !($i % 2) ? 1 : 2; $data[] = [ 'content' => $faker->text, 'user_id' => $user_id, ]; } $this->table('posts')->insert($data)->save(); } } ~~~ 上面代碼中,`$i % 2` 表示能否被 2 整除。 創建完畢后運行 `php think seed:run` 再次訪問 http://thinkphp.test/user/auth/read/id/1.html 就可以看到剛剛裝填好的假數據了。 ## 前端顯示 現在數據庫中已經有填充好的假數據,我們只需要在前端中輸出即可。 `application\user\controller\Auth.php` ~~~php public function read($id) { $user = User::with(['posts' => function ($query) { $query->limit(8); $query->order('created_at', 'desc'); }])->find($id); $this->assign([ 'user' => $user, 'session' => Session::get('user') ]); return $this->fetch(); } ~~~ 注意,`with(['posts' => function ($query)` 是一個閉包操作,下面的 `limit` 等語句都是針對關聯模型 `posts` 操作而不是 `User` 切換到前端頁面 `resources\views\user\auth\read.blade.php` ~~~html @extends('_layout.default') @section('title', $user->name) @section('content') <div class="panel panel-default list-group-flush"> <div class="panel-heading"> <h4> @if(!is_null($session) && $session->id === $user->id) <a class="btn btn-primary" href="{{ url('user/auth/edit', ['id' => session('user.id')]) }}"> 編輯資料 </a> 歡迎您 @else 您正在查看 @endif {{ $user->name }} </h4> </div> @foreach ($user->posts as $post) <div class="list-group-item"> <p> {{ $post->content }} </p> {{ $post->created_at }} </div> @endforeach </div> @stop ~~~ 再次訪問 http://thinkphp.test/user/auth/read/id/1.html 即可看到前端內容完整的被渲染出來了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看