Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4.  
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6.  
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Database\Eloquent\Collection;
  12. use Illuminate\Database\Eloquent\ModelNotFoundException;
  13.  
  14. class BelongsToManyFromColumn extends BelongsToMany
  15. {
  16.  
  17. public function __construct(Builder $query, Model $parent, $foreignKey, $relatedKey, $relationName = null)
  18. {
  19. parent::__construct($query, $parent, 'mybb_usergroups', $foreignKey, $relatedKey, $relationName);
  20. }
  21.  
  22. public function addEagerConstraints(array $models)
  23. {
  24. /*
  25. ORIGINAL:
  26. $this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
  27.  
  28. We replace this with the code below, because we already have the IDs from our models as opposed to doing an inner join.
  29. */
  30.  
  31. $key = (string)$this->relatedKey;
  32. $keys = collect($models)->pluck($key)->flatten()->unique()->all();
  33.  
  34. $this->query->whereIn($this->getQualifiedForeignKeyName(), $keys);
  35. }
  36.  
  37. public function addConstraints()
  38. {
  39. /*
  40. ORIGINAL:
  41. $this->performJoin();
  42.  
  43. if (static::$constraints) {
  44. $this->addWhereConstraints();
  45. }
  46.  
  47. We remove the performJoin() method because we already have the IDs from our model.
  48. */
  49. if (static::$constraints) {
  50. $this->addWhereConstraints();
  51. }
  52. }
  53.  
  54. protected function aliasedPivotColumns()
  55. {
  56. /*
  57. ORIGINAL:
  58. $defaults = [$this->foreignKey, $this->relatedKey];
  59.  
  60. return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) {
  61. return $this->table.'.'.$column.' as pivot_'.$column;
  62. })->unique()->all();
  63.  
  64. We use the foreignKey as the relatedKey because we don't really have a relatedKey(gids) on the table structure for our mybb_users group since we already have the IDs from the models
  65. */
  66. return [
  67. $this->table.'.'.$this->foreignKey.' as pivot_'.$this->foreignKey,
  68. $this->table.'.'.$this->foreignKey.' as pivot_'.$this->relatedKey,
  69. ];
  70. }
  71.  
  72. public function match(array $models, Collection $results, $relation)
  73. {
  74. /*
  75. There is quite a bit of modification here, you'll just have to read up the source and understand it but basically
  76. we get the list of groups from the buildDictionary function and then we go through the Eloquent models for the User model
  77. and attach any that are in the relatedKey(gids) array.
  78. */
  79. $dictionary = $this->buildDictionary($results);
  80.  
  81. foreach ($models as $model) {
  82. $collections = [];
  83. foreach($model->{$this->relatedKey} as $key) {
  84. if (isset($dictionary[$key])) {
  85. $collections[] = $dictionary[$key];
  86. }
  87. }
  88. $model->setRelation(
  89. $relation, $this->related->newCollection($collections)
  90. );
  91. }
  92.  
  93. return $models;
  94. }
  95.  
  96. protected function buildDictionary(Collection $results)
  97. {
  98. /*
  99. This justs creates an array and keys it by its ID (gid)
  100. */
  101. $dictionary = [];
  102.  
  103. foreach ($results as $result) {
  104. $dictionary[$result->pivot->{$this->foreignKey}] = $result;
  105. }
  106.  
  107. return $dictionary;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement