本文實(shí)例講述了Laravel5.1 框架關(guān)聯(lián)模型之后操作。分享給大家供大家參考,具體如下:
之前寫過(guò)關(guān)于模型關(guān)聯(lián)的筆記,但是模型關(guān)聯(lián)好后的一些使用沒(méi)有介紹,今天補(bǔ)上
我們準(zhǔn)備了兩個(gè)模型:Post和Comment。 它們的關(guān)系是一對(duì)多關(guān)系。現(xiàn)在我們要?jiǎng)?chuàng)建新的Comment到Post:
public function getIndex() { // 創(chuàng)建一個(gè)comment模型 $comment = new Comment(['title'=> 'comment1', 'content'=> 'content1']); // 取到post模型 $post = Post::findOrFail(1); $post->comments()->save($comment); }
這樣創(chuàng)建呢 Comment的post_id 列會(huì)自動(dòng)填充。
我們還可以批量的添加下屬模型,相當(dāng)方便~:
public function getIndex() { // 創(chuàng)建一個(gè)comment模型 $comment2 = new Comment(['title'=> 'comment2', 'content'=> 'content2']); $comment3 = new Comment(['title'=> 'comment3', 'content'=> 'content3']); // 取到post模型 $post = Post::findOrFail(1); $post->comments()->saveMany([$comment2, $comment3]); }
準(zhǔn)備一個(gè)Tag模型,它和Post模型是多對(duì)多的關(guān)系,別忘了生成中間表哦:
public function getIndex() { // 創(chuàng)建文章 $post = new Post(); $post->title = 'Laravel Model'; $post->sub_title = '模型的詳細(xì)使用'; $post->content = 'content...'; // 添加到Tag $tag = Tag::findOrFail(1); $tag->posts()->save($post); }
↑ 我們無(wú)需管中間表,Laravel會(huì)自動(dòng)為我們填充中間表的關(guān)聯(lián)屬性,
多對(duì)多的save方法中是允許我們傳入第二個(gè)參數(shù)的。第二個(gè)參數(shù)是中間表的屬性數(shù)組:
public function getIndex() { // 創(chuàng)建文章 $post = new Post(); $post->title = 'Laravel Model'; $post->sub_title = '模型的詳細(xì)使用'; $post->content = 'content...'; // 添加到Tag $tag = Tag::findOrFail(1); // 當(dāng)創(chuàng)建時(shí)需要填充中間表的額外列時(shí),可以傳遞第二個(gè)參數(shù)。 // 這里我們的中間表有個(gè)expires列,添加關(guān)聯(lián)時(shí)可以同時(shí)設(shè)置。 $tag->posts()->save($post, ['expires' => true]); }
Create方法是一種批量填充模式 所以記得在Model中設(shè)置白/黑名單,它和save的唯一區(qū)別就是 只能傳遞數(shù)組、不能將一個(gè)模型實(shí)例傳入。
public function getIndex() { $tag = Tag::findOrFail(1); // create方法同樣也可以接受第二個(gè)參數(shù)。 $tag->posts()->create([ 'title' => 'Laravel Model', 'sub_title' => 'Laravel 模型關(guān)聯(lián)的使用', 'content' => 'content...' ], ['expires' => true]); }
重要的事情需要重復(fù)一遍:associate方法只不對(duì)多對(duì)多關(guān)系適用。而且使用時(shí)要用下方模型 調(diào)用associate方法,將下方模型更新到新的上方模型。
public function getIndex() { $post = Post::findOrFail(1); $comment = Comment::findOrFail(1); $comment->post()->associate($post); $comment->save(); }
重要的事情需要重復(fù)一遍:dissociate方法只不對(duì)多對(duì)多關(guān)系適用。而且使用時(shí)要用下方模型 調(diào)用dissociate方法,將下方模型從上方模型的關(guān)聯(lián)中移除。此外此方法執(zhí)行后會(huì)將下方模型的外鍵id至為0。
public function getIndex() { $post = Post::findOrFail(1); $comment = Comment::findOrFail(1); $comment->post()->dissociate($post); $comment->save(); }
一定要看注釋,一定要看注釋,一定要看注釋,注釋解釋的很清楚,你可能心中有疑問(wèn) 這個(gè)追加關(guān)系和之間創(chuàng)建關(guān)系有什么區(qū)別?你可能忽視了一個(gè)細(xì)節(jié):創(chuàng)建添加時(shí) 是新建一個(gè)模型后加入關(guān)聯(lián),而attach方法是:追加一個(gè)已經(jīng)存在的模型進(jìn)行關(guān)聯(lián)。
public function getIndex() { // 取到ID為3的文章 這篇文章與id為1的tag有關(guān)系。 $post = Post::findOrFail(3); // attach方法的參數(shù)只需要傳遞id(整型)即可,中間表會(huì)自動(dòng)更新。 // 注意:attach的功能是追加一個(gè)關(guān)系并非更新,執(zhí)行以下代碼后 該post會(huì)與id為3和2的tag有關(guān)系。 $post->tags()->attach(2); $post->save(); }
當(dāng)追加關(guān)系時(shí)同樣也可以將一個(gè)中間表數(shù)據(jù)加入第二個(gè)參數(shù),以此更新中間表的其他列。
public function getIndex() { // 取到ID為3的文章 這篇文章與id為1的tag有關(guān)系。 $post = Post::findOrFail(3); // attach方法的參數(shù)只需要傳遞id(整型)即可,中間表會(huì)自動(dòng)更新。 // 注意:attach的功能是追加一個(gè)關(guān)系并非更新,執(zhí)行以下代碼后 該post會(huì)與id為3和2的tag有關(guān)系。 $post->tags()->attach(2, ['expires' => true]); }
批量追加:
public function getIndex() { $post = Post::findOrFail(3); // 第一個(gè)參數(shù)也可以接收一個(gè)數(shù)組。 $post->tags()->attach([2, ['expires' => true], 4, 6]); }
detach方法于attach方法相反,detach方法會(huì)將關(guān)聯(lián)關(guān)系刪除:
public function getIndex() { $post = Post::findOrFail(3); $post->tags()->detach(1); }
批量卸載:
public function getIndex() { $post = Post::findOrFail(3); $post->tags()->detach([1, 3, 5]); }
同步關(guān)系可謂是非常方便,具體的看注釋吧,寫的很清楚:
public function getIndex() { // 取出id為2的tag,此時(shí)它只和id為3的post有關(guān)聯(lián)。 $tag = Tag::findOrFail(2); // 同步:傳入一個(gè)id數(shù)組,存在于此數(shù)組的id都會(huì)被追加關(guān)系,而不在此數(shù)組中的id模型關(guān)聯(lián) 都會(huì)被移除。 $tag->posts()->sync([2, 4, 5]); }
注意:sync方法也可以傳入第二個(gè)參數(shù),也是數(shù)組類型 以便更新中間表中的其他列。由于語(yǔ)法跟前面幾個(gè)方法一樣,就不在重復(fù)寫了。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:婁底 寶雞 鎮(zhèn)江 湛江 黃南 銅川 南陽(yáng) 宜賓
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel5.1 框架關(guān)聯(lián)模型之后操作實(shí)例分析》,本文關(guān)鍵詞 Laravel5.1,框架,關(guān)聯(lián),模型,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。