Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- class BelongsToManyFromColumn extends BelongsToMany
- {
- public function __construct(Builder $query, Model $parent, $foreignKey, $relatedKey, $relationName = null)
- {
- parent::__construct($query, $parent, 'mybb_usergroups', $foreignKey, $relatedKey, $relationName);
- }
- public function addEagerConstraints(array $models)
- {
- /*
- ORIGINAL:
- $this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
- We replace this with the code below, because we already have the IDs from our models as opposed to doing an inner join.
- */
- $key = (string)$this->relatedKey;
- $keys = collect($models)->pluck($key)->flatten()->unique()->all();
- $this->query->whereIn($this->getQualifiedForeignKeyName(), $keys);
- }
- public function addConstraints()
- {
- /*
- ORIGINAL:
- $this->performJoin();
- if (static::$constraints) {
- $this->addWhereConstraints();
- }
- We remove the performJoin() method because we already have the IDs from our model.
- */
- if (static::$constraints) {
- $this->addWhereConstraints();
- }
- }
- protected function aliasedPivotColumns()
- {
- /*
- ORIGINAL:
- $defaults = [$this->foreignKey, $this->relatedKey];
- return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) {
- return $this->table.'.'.$column.' as pivot_'.$column;
- })->unique()->all();
- 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
- */
- return [
- $this->table.'.'.$this->foreignKey.' as pivot_'.$this->foreignKey,
- $this->table.'.'.$this->foreignKey.' as pivot_'.$this->relatedKey,
- ];
- }
- public function match(array $models, Collection $results, $relation)
- {
- /*
- There is quite a bit of modification here, you'll just have to read up the source and understand it but basically
- we get the list of groups from the buildDictionary function and then we go through the Eloquent models for the User model
- and attach any that are in the relatedKey(gids) array.
- */
- $dictionary = $this->buildDictionary($results);
- foreach ($models as $model) {
- $collections = [];
- foreach($model->{$this->relatedKey} as $key) {
- if (isset($dictionary[$key])) {
- $collections[] = $dictionary[$key];
- }
- }
- $model->setRelation(
- $relation, $this->related->newCollection($collections)
- );
- }
- return $models;
- }
- protected function buildDictionary(Collection $results)
- {
- /*
- This justs creates an array and keys it by its ID (gid)
- */
- $dictionary = [];
- foreach ($results as $result) {
- $dictionary[$result->pivot->{$this->foreignKey}] = $result;
- }
- return $dictionary;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement