Grouping by pivot table columns

Posted 1 week ago by untymage

Have this collection (on $thread->tags) :

Collection {#520 ▼
  #items: array:2 [▼
    0 => Tag {#505 ▼
      #relations: array:1 [▼
        "pivot" => Pivot {#506 ▼
          #attributes: array:3 [▼
            "thread_id" => 1
            "tag_id" => 1
            "tag_type" => "main"
    1 => Tag {#505 ▼
      #relations: array:1 [▼
        "pivot" => Pivot {#506 ▼
          #attributes: array:3 [▼
            "thread_id" => 1
            "tag_id" => 1
            "tag_type" => "custom"
  ]
}

and how do i group this colletion by tag_typein the pivot table ? result should be something like this one :

Collection {#490 ▼
  #items: array:2 [▼
    "main" => Collection {#489 ▼
      #items: array:1 [▼
        0 => Tag {#493 ▶}
       1 => Tag {#494 ▶}
      ]
    },
   "custom" => Collection {#489 ▼
      #items: array:1 [▼
        0 => Tag {#493 ▶}
  ]
}
$tagTypes = $thread->tags->filter->pivot->groupBy('tag_type');

which is doesnt work and gave me a unnamed key in array:

Collection {#511 ▼
  #items: array:1 [▼
    "" => Collection {#520 ▶}
  ]
}

any idea?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.