<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] 多態:是指并不明確指向和誰關聯,也就是說可以和任何庫進行關聯 示例表結構 文章有評論,視頻也有評論,共用評論表 comments 評論表 | 鍵名 | 類型 | | --- | --- | | id | PK | | title | varchar(200) | | table_type | varchar(200) | | table_id | integer | | created_at | timestamp | | updated_at | timestamp | # 實現 Post.php ~~~ public function comments() { return $this->morphMany(Comment::class, 'table'); } ~~~ Video.php ``` public function comments() { return $this->morphMany(Comment::class,'table'); } ``` Comment.php ``` public function commentable() { return $this->morphTo('table'); } ``` Comment表中一些數據 ``` table_id就是對應Video或者Post表中的id table_type就是對應他們的命名空間,比如App\Models\Post ``` > 字段中的 table_type、table_id 的前綴table,對應 morphMany、morphTo 中的參數 > $this->morphMany > 加到任意Model,那個Model就具備了評論的功能,很方便 # 操作 ## 查 Post查評論 ``` $post=Post::find(1); $post->comments->toArray(); ``` Comment查對應的關聯 ``` $comment=Comment::find(2); $comment->commentable; ``` ## 新增 比如,為第二條文章新增一條數據 ``` Post::find(2)->comments()->create(['title'=>'new data']); ``` ## 補充 在 AppServiceProvider boot 方法中自定義多態關聯的類型字段。\ 這個可以按照項目需求來寫 ~~~ use App\Models\Post; use App\Models\Video; use Illuminate\Database\Eloquent\Relations\Relation; public function boot() { $this->bootEloquentMorphs(); } /** * 自定義多態關聯的類型字段 */ private function bootEloquentMorphs() { Relation::morphMap([ Post::TABLE => Post::class, Video::TABLE => Video::class, ]); } ~~~ 參考[1對多](http://www.hmoore.net/jdxia/laravel5note/388604)
                  <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>

                              哎呀哎呀视频在线观看