亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel5.1 框架關聯(lián)模型之后操作實例分析

 更新時間:2020年01月09日 11:54:06   作者:Sky_sunkang  
這篇文章主要介紹了Laravel5.1 框架關聯(lián)模型之后操作,結合實例形式分析了laravel5.1框架寫入關聯(lián)模型、更新關聯(lián)關系等相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了Laravel5.1 框架關聯(lián)模型之后操作。分享給大家供大家參考,具體如下:

之前寫過關于模型關聯(lián)的筆記,但是模型關聯(lián)好后的一些使用沒有介紹,今天補上

1 寫入關聯(lián)模型

1.1 使用Save方法(一對多)

我們準備了兩個模型:Post和Comment。 它們的關系是一對多關系。現(xiàn)在我們要創(chuàng)建新的Comment到Post:

  public function getIndex()
  {
    // 創(chuàng)建一個comment模型
    $comment = new Comment(['title'=> 'comment1', 'content'=> 'content1']);
    // 取到post模型
    $post = Post::findOrFail(1);
    $post->comments()->save($comment);
  }

這樣創(chuàng)建呢 Comment的post_id 列會自動填充。

我們還可以批量的添加下屬模型,相當方便~:

  public function getIndex()
  {
    // 創(chuàng)建一個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]);
  }

1.2 使用Save方法(多對多)

準備一個Tag模型,它和Post模型多對多的關系,別忘了生成中間表哦:

  public function getIndex()
  {
    // 創(chuàng)建文章
    $post = new Post();
    $post->title = 'Laravel Model';
    $post->sub_title = '模型的詳細使用';
    $post->content = 'content...';
    // 添加到Tag
    $tag = Tag::findOrFail(1);
    $tag->posts()->save($post);
  }

↑ 我們無需管中間表,Laravel會自動為我們填充中間表的關聯(lián)屬性,

多對多的save方法中是允許我們傳入第二個參數(shù)的。第二個參數(shù)是中間表的屬性數(shù)組:

  public function getIndex()
  {
    // 創(chuàng)建文章
    $post = new Post();
    $post->title = 'Laravel Model';
    $post->sub_title = '模型的詳細使用';
    $post->content = 'content...';
    // 添加到Tag
    $tag = Tag::findOrFail(1);
    // 當創(chuàng)建時需要填充中間表的額外列時,可以傳遞第二個參數(shù)。
    // 這里我們的中間表有個expires列,添加關聯(lián)時可以同時設置。
    $tag->posts()->save($post, ['expires' => true]);
  }

1.3 使用Create方法

Create方法是一種批量填充模式 所以記得在Model中設置白/黑名單,它和save的唯一區(qū)別就是 只能傳遞數(shù)組、不能將一個模型實例傳入。

  public function getIndex()
  {
    $tag = Tag::findOrFail(1);
    // create方法同樣也可以接受第二個參數(shù)。
    $tag->posts()->create([
      'title' => 'Laravel Model',
      'sub_title' => 'Laravel 模型關聯(lián)的使用',
      'content' => 'content...'
    ], ['expires' => true]);
  }

2 更新關聯(lián)關系

2.1 更新一個關系(除多對多適用)

重要的事情需要重復一遍:associate方法只不對多對多關系適用。而且使用時要用下方模型 調用associate方法,將下方模型更新到新的上方模型

  public function getIndex()
  {
    $post = Post::findOrFail(1);
    $comment = Comment::findOrFail(1);
    $comment->post()->associate($post);
    $comment->save();
  }

2.2 移除一個關系(除多對多適用)

重要的事情需要重復一遍:dissociate方法只不對多對多關系適用。而且使用時要用下方模型 調用dissociate方法,將下方模型從上方模型的關聯(lián)中移除。此外此方法執(zhí)行后會將下方模型的外鍵id至為0。

  public function getIndex()
  {
    $post = Post::findOrFail(1);
    $comment = Comment::findOrFail(1);
    $comment->post()->dissociate($post);
    $comment->save();
  }

2.3 追加一個關系(多對多關系)

一定要看注釋,一定要看注釋,一定要看注釋,注釋解釋的很清楚,你可能心中有疑問 這個追加關系和之間創(chuàng)建關系有什么區(qū)別?你可能忽視了一個細節(jié):創(chuàng)建添加時 是新建一個模型后加入關聯(lián),而attach方法是:追加一個已經(jīng)存在的模型進行關聯(lián)。

  public function getIndex()
  {
    // 取到ID為3的文章 這篇文章與id為1的tag有關系。
    $post = Post::findOrFail(3);
    // attach方法的參數(shù)只需要傳遞id(整型)即可,中間表會自動更新。
    // 注意:attach的功能是追加一個關系并非更新,執(zhí)行以下代碼后 該post會與id為3和2的tag有關系。
    $post->tags()->attach(2);
    $post->save();
  }

當追加關系時同樣也可以將一個中間表數(shù)據(jù)加入第二個參數(shù),以此更新中間表的其他列。

  public function getIndex()
  {
    // 取到ID為3的文章 這篇文章與id為1的tag有關系。
    $post = Post::findOrFail(3);
    // attach方法的參數(shù)只需要傳遞id(整型)即可,中間表會自動更新。
    // 注意:attach的功能是追加一個關系并非更新,執(zhí)行以下代碼后 該post會與id為3和2的tag有關系。
    $post->tags()->attach(2, ['expires' => true]);
  }

批量追加:

  public function getIndex()
  {
    $post = Post::findOrFail(3);
    // 第一個參數(shù)也可以接收一個數(shù)組。
    $post->tags()->attach([2, ['expires' => true], 4, 6]);
  }

2.4 卸載一個關系(多對多關系)

detach方法于attach方法相反,detach方法會將關聯(liá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]);
  }

2.5 同步關系

同步關系可謂是非常方便,具體的看注釋吧,寫的很清楚:

  public function getIndex()
  {
    // 取出id為2的tag,此時它只和id為3的post有關聯(lián)。
    $tag = Tag::findOrFail(2);
    // 同步:傳入一個id數(shù)組,存在于此數(shù)組的id都會被追加關系,而不在此數(shù)組中的id模型關聯(lián) 都會被移除。
    $tag->posts()->sync([2, 4, 5]);
  }

注意:sync方法也可以傳入第二個參數(shù),也是數(shù)組類型 以便更新中間表中的其他列。由于語法跟前面幾個方法一樣,就不在重復寫了。

更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

相關文章

最新評論