Laravel deep links


SUBMITTED BY: delsol1

DATE: March 7, 2017, 3:08 p.m.

FORMAT: Text only

SIZE: 3.4 kB

HITS: 555

  1. A page has multiple elements and they are linked using the page_to_elements table. Each element has multiple element_fields and are linked using the element_to_element_fields. Each element_field has a type and are linked using the element_to_element_fields table. The values of each element_field within the element has a value (eitehr in value_char, value_text or value_num) that is stored in the element_values table.
  2. Below is how the database structure is:
  3. pages:
  4. id|name
  5. elements:
  6. id|name
  7. element_fields_types (sql_type can be char, text or num):
  8. id|name|sql_type
  9. element_fields (names can be title, intro, content, link, number, etc etc):
  10. id:element_field_type_id|name
  11. element_to_element_fields:
  12. id|element_id|element_field_id
  13. page_to_elements:
  14. id|page_id|element_id
  15. element_values:
  16. id|page_id|element_id|page_to_element_id|element_field_id|value_char|value_text|value_num
  17. What I am looking for is a good hasManyToMany solution to get all values when I request a page id. I now have multiple loops and array creations to get a structure like this (where the values are from the correct column name based on what was set in the element_fields):
  18. $page = array(
  19. 'elements' => array(
  20. [0] => array(
  21. 'element_name_here' => array(
  22. 'fields' => array(
  23. [0] => array(
  24. 'field_name_here' => 'Field value',
  25. 'field_name_here' => 'Field value',
  26. 'field_name_here' => 'Field value',
  27. 'field_name_here' => 'Field value'
  28. ),
  29. [1] => array(
  30. 'field_name_here' => 'Field value',
  31. 'field_name_here' => 'Field value',
  32. 'field_name_here' => 'Field value',
  33. 'field_name_here' => 'Field value'
  34. ),
  35. [2] => array(
  36. 'field_name_here' => 'Field value',
  37. 'field_name_here' => 'Field value',
  38. 'field_name_here' => 'Field value',
  39. 'field_name_here' => 'Field value'
  40. ),
  41. )
  42. )
  43. ),
  44. [1] => array(
  45. 'element_name_here' => array(
  46. 'fields' => array(
  47. [0] => array(
  48. 'field_name_here' => 'Field value',
  49. 'field_name_here' => 'Field value',
  50. 'field_name_here' => 'Field value',
  51. 'field_name_here' => 'Field value'
  52. ),
  53. [1] => array(
  54. 'field_name_here' => 'Field value',
  55. 'field_name_here' => 'Field value',
  56. 'field_name_here' => 'Field value',
  57. 'field_name_here' => 'Field value'
  58. ),
  59. [2] => array(
  60. 'field_name_here' => 'Field value',
  61. 'field_name_here' => 'Field value',
  62. 'field_name_here' => 'Field value',
  63. 'field_name_here' => 'Field value'
  64. ),
  65. )
  66. )
  67. ),
  68. )
  69. );
  70. And we will get something like
  71. $page = Page::find($id);
  72. print_r($page->getValues->toArray());

comments powered by Disqus