Inheritance: extends Illuminate\Database\Eloquent\Relations\Relation
Example #1
6
 /**
  * @param QueryBuilder $builder
  * @param $data
  *
  * @return void
  */
 public function applyFilterConstraint(QueryBuilder $builder, $data)
 {
     if (empty($data)) {
         return;
     }
     $builder->whereIn($this->relation->getForeignKey(), explode(',', $data));
 }
 protected function joinBelongsToRelation(Relations\BelongsTo $relation, $type)
 {
     $table = $relation->getRelated()->getTable();
     $foreignKey = $relation->getQualifiedForeignKey();
     $localKey = $relation->getQualifiedOtherKeyName();
     $this->query->join($table, $foreignKey, '=', $localKey, $type);
 }
Example #3
2
 protected function applyBelongsTo(Query $query, Model $model, BelongsTo $belongsTo, $name)
 {
     if (isset($this->joinClasses[$name])) {
         return;
     }
     $modelTable = '';
     // If the table has already an alias
     if (str_contains($name, '.')) {
         $path = explode('.', $name);
         array_pop($path);
         $parentPath = implode('.', $path);
         if (isset($this->joinAliases[$parentPath])) {
             $modelTable = $this->joinAliases[$parentPath];
         }
     }
     if (!$modelTable) {
         $modelTable = $belongsTo->getParent()->getTable();
     }
     $related = $belongsTo->getRelated();
     $relatedTable = $related->getTable();
     $foreignKey = $belongsTo->getForeignKey();
     $otherKey = $belongsTo->getOtherKey();
     $alias = $this->joinNameToAlias($name);
     $joinMethod = $this->getJoinMethod($name);
     $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$modelTable}.{$foreignKey}", '=', "{$alias}.{$otherKey}");
     $query->distinct();
     $this->addQueryColumn("{$modelTable}.{$foreignKey}");
     $this->joinClasses[$name] = $belongsTo->getRelated();
     $this->joinTable[$name] = $relatedTable;
     $this->joinAliases[$name] = $alias;
 }
Example #4
1
 /**
  * Create a new belongs to relationship instance.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  \Illuminate\Database\Eloquent\Model  $parent
  * @param  string  $foreignKey
  * @param  string  $otherKey
  * @param  string  $type
  * @param  string  $relation
  * @return void
  */
 public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $type, $relation)
 {
     $this->morphType = $type;
     parent::__construct($query, $parent, $foreignKey, $otherKey, $relation);
 }
Example #5
1
 /**
  * Handle dynamic method calls to the relationship.
  *
  * @param string $method
  * @param array  $parameters
  *
  * @return mixed
  */
 public function __call($method, $parameters)
 {
     $result = parent::__call($method, $parameters);
     if ($method !== 'get') {
         call_user_func_array([$this->fallbackQuery, $method], $parameters);
     }
     return $result;
 }
 private function generateNullConditionFromBelongsTo(BelongsTo $relation)
 {
     $this->query = $this->query->whereNull($relation->getQualifiedOtherKeyName());
 }
Example #7
1
 /**
  * Add an object through a BelongsTo relation
  *
  * @param \Illuminate\Database\Eloquent\Relations\BelongsTo $relation
  * @param \Illuminate\Database\Eloquent\Model $model
  * @return bool
  */
 protected function addBelongsToAssociatedObject(BelongsTo $relation, \Illuminate\Database\Eloquent\Model $model)
 {
     $relation->associate($model);
     return $this->save();
 }
Example #8
1
 /**
  * Makes a raw where condition string from a BelongsTo relation instance.
  *
  * @param BelongsTo $relation
  * @return string
  */
 protected function getConditionStringFromBelongsTo(BelongsTo $relation)
 {
     $id = $this->getAttribute($relation->getForeignKey());
     // an empty foreign key will, as a null-scope, remove the item from any list
     if (null === $id) {
         return null;
     }
     return '`' . $relation->getForeignKey() . '` = ' . (int) $id;
 }
Example #9
1
 public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $relationName)
 {
     $this->relationName = $relationName;
     parent::__construct($query, $parent, $foreignKey, $otherKey, $relationName);
 }
Example #10
1
 public static function setBelongsTo(BelongsTo $belongsTo, $object = null)
 {
     return $object ? $belongsTo->associate($object) : $belongsTo->dissociate();
 }
Example #11
1
 /**
  * Add select with alias to query from "belongs to" relation
  *
  * @param Builder   $query
  * @param BelongsTo $relatedModel
  */
 private function addBelongsToSelect(Builder $query, BelongsTo $relatedModel)
 {
     $query->getQuery()->join($relatedModel->getRelated()->getTable(), $relatedModel->getQualifiedForeignKey(), '=', $relatedModel->getQualifiedOtherKeyName());
 }
Example #12
0
 /**
  * Handle dynamic method calls to the relationship.
  *
  * @param  string  $method
  * @param  array   $parameters
  * @return mixed
  */
 public function __call($method, $parameters)
 {
     try {
         return parent::__call($method, $parameters);
     } catch (BadMethodCallException $e) {
         $this->macroBuffer[] = compact('method', 'parameters');
         return $this;
     }
 }