Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法實(shí)例分析
本文實(shí)例講述了Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法。分享給大家供大家參考,具體如下:
什么是多態(tài)關(guān)聯(lián)? 一個(gè)例子你就明白了:好比如說評(píng)論 它可以屬于視頻類 也可以屬于文章類,當(dāng)有個(gè)需求是 從評(píng)論表中取到視頻類的數(shù)據(jù),這就需要用到多態(tài)關(guān)聯(lián)了。
簡單的一句話總結(jié):一張表對(duì)應(yīng)兩張表。
1 實(shí)現(xiàn)多態(tài)關(guān)聯(lián)
1.1 文章表的結(jié)構(gòu)
public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body');$table->timestamps(); }); }
1.2 視頻表結(jié)構(gòu)
public function up() { Schema::create('videos', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->timestamps(); }); }
1.3 評(píng)論表結(jié)構(gòu)
public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->text('content'); $table->integer('item_id'); $table->string('item_type'); $table->timestamps(); }); }
↑ 這里需要指定 item_id 和 item_type 單一介紹一下 item_type 它主要是區(qū)別關(guān)聯(lián)于那張表的 我們這里它只有兩個(gè)值:App\Article 或 App\Video。
1.4 編寫多態(tài)關(guān)聯(lián)
Article 和 Video:
public function comments() { /** * 第二個(gè)參數(shù):如果你的前綴是item_ 那么就寫item 如果是別的就寫別的。 * 第三個(gè)參數(shù):item_type * 第四個(gè)參數(shù):item_id * 第五個(gè)參數(shù):關(guān)聯(lián)到那個(gè)表的鍵 * (以上除了第二個(gè)參數(shù)都可以省略) */ return $this->morphMany(Comment::class, 'item', 'item_type', 'item_id', 'id'); }
Comment:
public function video() { /** * 三個(gè)參數(shù)都可以省略 不過K建議你還是寫全 */ return $this->morphTo('item', 'item_type', 'item_id'); }
使用:
Route::get('/', function () { $video = App\Video::find(8); foreach ($video->comments as $comment) { echo $comment->id . ": " . $comment->item_type; } });
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
上一篇:PHP變量作用域(全局變量&局部變量)&global&static關(guān)鍵字
欄 目:PHP編程
下一篇:php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析
本文標(biāo)題:Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法實(shí)例分析
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/10965.html
您可能感興趣的文章
- 01-11thinkphp框架類庫擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問題的分析與解決
- 01-11php 實(shí)現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel框架基礎(chǔ)語法與知識(shí)點(diǎn)整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實(shí)現(xiàn)方法分析
- 01-11laravel5.5框架的上傳圖片功能實(shí)例分析【僅傳到服務(wù)器端】


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請(qǐng)求數(shù)據(jù) php本地?cái)?shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請(qǐng)求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計(jì)方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)