Illuminate\Database\Eloquent\Builder::getRelation PHP Метод

getRelation() публичный Метод

Get the relation instance for the given relation name.
public getRelation ( string $name ) : Illuminate\Database\Eloquent\Relations\Relation
$name string
Результат Illuminate\Database\Eloquent\Relations\Relation
    public function getRelation($name)
    {
        // We want to run a relationship query without any constrains so that we will
        // not have to remove these where clauses manually which gets really hacky
        // and is error prone while we remove the developer's own where clauses.
        $relation = Relation::noConstraints(function () use($name) {
            try {
                return $this->getModel()->{$name}();
            } catch (BadMethodCallException $e) {
                throw RelationNotFoundException::make($this->getModel(), $name);
            }
        });
        $nested = $this->nestedRelations($name);
        // If there are nested relationships set on the query, we will put those onto
        // the query instances so that they can be handled after this relationship
        // is loaded. In this way they will all trickle down as they are loaded.
        if (count($nested) > 0) {
            $relation->getQuery()->with($nested);
        }
        return $relation;
    }

Usage Example

Пример #1
0
 /**
  * Get queries to fetch relationships.
  *
  * @param  Builder  $builder
  * @param  array    $models
  * @param  string   $name
  * @param  Closure  $constraints
  * @return array
  */
 protected function getQueries(Builder $builder, array $models, $name, Closure $constraints)
 {
     return collect($models)->map(function ($model) use($builder, $name, $constraints) {
         $relation = $builder->getRelation($name);
         $relation->addEagerConstraints([$model]);
         call_user_func_array($constraints, [$relation, $model]);
         if (method_exists($relation, 'getSelectColumns')) {
             $r = new ReflectionMethod(get_class($relation), 'getSelectColumns');
             $r->setAccessible(true);
             $select = $r->invoke($relation, ['*']);
             $relation->addSelect($select);
         }
         $relation->initRelation([$model], $name);
         return $relation;
     });
 }
All Usage Examples Of Illuminate\Database\Eloquent\Builder::getRelation