Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Laravel] Relacja hasMany pomiedzy tabela Produkt i ProductStructure, Potrzebuje czegoś jak GROUP_CONCAT w MySQL
fumfel20
post 13.06.2019, 21:06:55
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 17.09.2015

Ostrzeżenie: (0%)
-----


Witam. Mam tabele Produkt w stylu
ID, PART_NO, DESCRIPTION ..ITP.
1 RX.300.123.IW, 3000x1220x15 Itallian Walnut


Mam też tabelę ProductStructure która wyglada tak:
ID, PART_NO , COMPONENT PART
1 , RX.300.123.CW , RX.905.122.RC
2 , RX.300.123.CW , RX.666.305.WC

Oznacza to ze produkt z ID 1 sklada sie z dwóch komponentów. w czystym SQL nie ma problemu, robie group_concat i te dwa componeny robia mi sie stringiem.
Chciałbym jednak zrobić to w Eloquent czyli wyswietlic w tabeli produkty, kolumne z komponentami ale w jednym wierszu.

Pomoże ktoś to ogarnąć? Czy moze zrobic po prostu IMPLODE w widoku i jazda? Pozdrawiam
EDIT:
Dodam tylko ze jak pobieram tylko jeden problem to relacja dump wyglada tak:
  1. Collection {#1477 ?
  2. #items: array:1 [?
  3. 0 => Product {#1476 ?
  4. #table: "Products.tblProduct"
  5. #guarded: []
  6. #connection: "mysql"
  7. #primaryKey: "id"
  8. #keyType: "int"
  9. +incrementing: true
  10. #with: []
  11. #withCount: []
  12. #perPage: 15
  13. +exists: true
  14. +wasRecentlyCreated: false
  15. #attributes: array:15 [?]
  16. #original: array:15 [?]
  17. #changes: []
  18. #casts: []
  19. #dates: []
  20. #dateFormat: null
  21. #appends: []
  22. #dispatchesEvents: []
  23. #observables: []
  24. #relations: array:1 [?
  25. "productStructure" => Collection {#1480 ?
  26. #items: array:2 [?
  27. 0 => ProductStructure {#1513 ?
  28. #table: "Products.tblProductStructure"
  29. #guarded: []
  30. #connection: "mysql"
  31. #primaryKey: "id"
  32. #keyType: "int"
  33. +incrementing: true
  34. #with: []
  35. #withCount: []
  36. #perPage: 15
  37. +exists: true
  38. +wasRecentlyCreated: false
  39. #attributes: array:6 [?
  40. "id" => 261
  41. "part_no" => "RX.300.123.IW"
  42. "component_part" => "RX.905.122.RC"
  43. "component_part_description" => "3050x1220x15 Raw/Raw MDF"
  44. "created_at" => "2019-06-06 20:46:01"
  45. "updated_at" => "2019-06-06 20:46:01"
  46. ]
  47. #original: array:6 [?]
  48. #changes: []
  49. #casts: []
  50. #dates: []
  51. #dateFormat: null
  52. #appends: []
  53. #dispatchesEvents: []
  54. #observables: []
  55. #relations: []
  56. #touches: []
  57. +timestamps: true
  58. #hidden: []
  59. #visible: []
  60. #fillable: []
  61. }
  62. 1 => ProductStructure {#1514 ?}
  63. ]
  64. }
  65. ]
  66. #touches: []
  67. +timestamps: true
  68. #hidden: []
  69. #visible: []
  70. #fillable: []
  71. }
  72. ]
  73. }


Czyli relacja dziala prawidlowo, jak tylko to co jest w productStructure wyswietlic w jednej linii? Potrzebuj z tego tylko component_part

EDIT:
Dla potomnych. Okazalo sie to dość proste. W widoku w petli foreach trzeba bylo to zapisać tak:
  1. {{ $item->productStructure->pluck('component_part')->implode(', ') }}


Ten post edytował fumfel20 13.06.2019, 21:07:33
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.03.2024 - 04:04