php Laravel中的多对多关系来自一系列ID Raw

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel中的多对多关系来自一系列ID Raw相关的知识,希望对你有一定的参考价值。

<?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;
    }
}

以上是关于php Laravel中的多对多关系来自一系列ID Raw的主要内容,如果未能解决你的问题,请参考以下文章

从laravel中的多对多关系中获取单列

Laravel 4:具有额外关系的多对多

如何在Laravel中的多对多关系中添加“别名”?

在 Laravel 中返​​回多对多关系的统一 JSON

Laravel 8:在空的多对多关系中使用枢轴时出错

渴望加载 Laravel 5 与两个外键的多对多关系