2016-06-16 33 views
0

我已經從我的數據庫belongsToMany關係沿着加載模型後:修改相關模型的集合執行查詢

$author = Author::with('publications')->first(); 

/** Returns something like: 

{ 
    "id": 3457, 
    "email": "[email protected]", 
    "publications": { 
     "1": { 
      "id": 240897, 
      "title": "Food left by other people at a restaurant - is it safe to eat? A comparative review.", 
      "journal": "Journal of Scrounging and Gleaning", 
      "year": 2007, 
      "pivot": { 
       "author_id": 3457, 
       "publication_id": 240897 
      } 
     }, 
     "2": { 
      "id": 249196, 
      "title": "Stop picking at it - you'll leave a scar!", 
      "journal": "Proceedings of the International Conference on Nagging", 
      "year": 2008, 
      "pivot": { 
       "author_id": 3457, 
       "publication_id": 249196 
      } 
     } 
    } 
} 

*/ 

我再從第三方API取每個publication一些額外的數據和將它合併到我的收藏集中,效果很好。

不過,我則希望在此基礎上第三方數據的出版物進行排序,所以我用sortByDesc回調:

$sorted = $author->publications->sortByDesc(function ($publication, $key) { 
    // Blah blah blah not important how I sort 
    return $blah; 
}); 

$author->publications = $sorted->values(); 

按照文檔的sortBy,我需要使用values如果我想要在排序後對結果重新編號。 $sorted->values()確實似乎是一個重新鍵入,排序的列表,但即使在將其分配給$author->publications之後,$author->publications仍然具有舊密鑰。

更奇怪的是,剛剛運行的sortByDesc似乎就地排序了列表,即使我沒有將結果返回給$author->publications。爲什麼我不能將我的重新鍵控收藏重新分配給$author->publications?我有一種感覺,這與關係與屬性之間的細微差別有關,但我不知道如何解決這個問題。

回答

1

的問題是,當我指定分類收集到$author->publication,我不是真正覆蓋關係$author->publication。相反,我只是添加一個新的屬性$author,這也恰好被稱爲publication。因此,我$author對象最終看起來是這樣的:

UserFrosting\Author Object 
(
    [timestamps] => 
    [connection:protected] => 
    [table:protected] => author 
    [primaryKey:protected] => id 
    [perPage:protected] => 15 
    [incrementing] => 1 
    [attributes:protected] => Array 
     (
      [id] => 3457 
      [email] => [email protected] 
      [publications] => Illuminate\Database\Eloquent\Collection Object 
       (
        [items:protected] => Array 
         (
          [0] => UserFrosting\Publication Object 
           (
            [timestamps] => 
            [connection:protected] => 
            [table:protected] => publication 
            [primaryKey:protected] => id 
            [perPage:protected] => 15 
            [incrementing] => 1 
            [attributes:protected] => Array 
             (
              [id] => 240897 
              [title] => Food left by other people at a restaurant - is it safe to eat? A comparative review. 
              [journal] => Journal of Scrounging and Gleaning 
              [year] => 2007 
             ) 
           ) 
          [1] => UserFrosting\Publication Object 
           (
            [timestamps] => 
            [connection:protected] => 
            [table:protected] => publication 
            [primaryKey:protected] => id 
            [perPage:protected] => 15 
            [incrementing] => 1 
            [attributes:protected] => Array 
             (
              [id] => 249196 
              [title] => Stop picking at it - you'll leave a scar! 
              [journal] => Proceedings of the International Conference on Nagging 
              [year] => 2008 
             ) 
           )         
         ) 
       ) 
     ) 

    [relations:protected] => Array 
     (
      [publications] => Illuminate\Database\Eloquent\Collection Object 
       (
        [items:protected] => Array 
         (
          [1] => UserFrosting\Publication Object 
           (
            [timestamps] => 
            [connection:protected] => 
            [table:protected] => publication 
            [primaryKey:protected] => id 
            [perPage:protected] => 15 
            [incrementing] => 1 
            [attributes:protected] => Array 
             (
              [id] => 240897 
              [title] => Food left by other people at a restaurant - is it safe to eat? A comparative review. 
              [journal] => Journal of Scrounging and Gleaning 
              [year] => 2007 
             ) 
           ) 
          [0] => UserFrosting\Publication Object 
           (
            [timestamps] => 
            [connection:protected] => 
            [table:protected] => publication 
            [primaryKey:protected] => id 
            [perPage:protected] => 15 
            [incrementing] => 1 
            [attributes:protected] => Array 
             (
              [id] => 249196 
              [title] => Stop picking at it - you'll leave a scar! 
              [journal] => Proceedings of the International Conference on Nagging 
              [year] => 2008 
             ) 
           ) 
         ) 
       ) 
     ) 
) 

正如你所看到的,relations包含出版物的分類列表中,但仍保留原來的鑰匙。 attributes包含排序的重新編號的列表。當我撥打$author->toArray()時,顯然使用關係而不是屬性。因此,訣竅是迫使Eloquent變爲assign my renumbered collection to the relation

$author->setRelation('publications', $sorted->values());