belongsTo() public method

Define an inverse one-to-one or many relationship.
public belongsTo ( string $related, string $foreignKey = null, string $otherKey = null, string $relation = null ) : BelongsTo
$related string
$foreignKey string
$otherKey string
$relation string
return Illuminate\Database\Eloquent\Relations\BelongsTo
Exemplo n.º 1
0
 public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
 {
     return parent::belongsTo($related, $foreignKey, $this->isOracleModel ? strtolower($otherKey) : $otherKey, $relation);
 }
Exemplo n.º 2
0
 public function belongsTo($string = null, $foreign_key = null, $other_key = null, $relation = null)
 {
     if (is_null($relation)) {
         $backtrace = debug_backtrace(false, 5);
         $caller = $backtrace[4];
         $relation = snake_case($caller['function']);
     }
     return parent::belongsTo($string, $foreign_key, $other_key, $relation);
 }
Exemplo n.º 3
0
 /**
  * Override for different naming convention
  *
  * @param  string $related
  * @param  string $foreignKey
  * @param  string $otherKey
  * @param  string $relation
  * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  */
 public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
 {
     // If no relation name was given, we will use this debug backtrace to extract
     // the calling method's name and use that as the relationship name as most
     // of the time this will be what we desire to use for the relationships.
     if (is_null($relation)) {
         list($current, $caller) = debug_backtrace(false, 2);
         $relation = $caller['function'];
     }
     $specialType = $this->getCmsSpecialRelationType($relation);
     // If no foreign key was supplied, we can use a backtrace to guess the proper
     // foreign key name by using the name of the relationship function, which
     // when combined with an "_id" should conventionally match the columns.
     if (is_null($foreignKey)) {
         if ($specialType === self::RELATION_TYPE_CATEGORY) {
             $foreignKey = config('pxlcms.relations.categories.keys.category');
         } else {
             $foreignKey = Str::snake($relation);
         }
     }
     // If the relation name is the same (casing aside) as the foreign key attribute,
     // we must make sure that the foreign key property is at least a key in the
     // attributes array. If it is not, there will be recursive lookup problems
     // where the attribute is never resolved and 'property does not exist' exceptions occur.
     if (snake_case($relation) == $foreignKey && !array_key_exists($foreignKey, $this->attributes)) {
         $this->attributes[$foreignKey] = null;
     }
     /** @var Builder $belongsTo */
     $belongsTo = parent::belongsTo($related, $foreignKey, $otherKey, $relation);
     // category relation must be filtered by module id
     if ($specialType === self::RELATION_TYPE_CATEGORY) {
         $belongsTo->where(config('pxlcms.relations.categories.keys.module'), $this->getModuleNumber());
     }
     return $belongsTo;
 }
Exemplo n.º 4
0
 /**
  * Define an inverse one-to-one or many relationship.
  *
  * @param  string  $related
  * @param  string  $foreignKey
  * @param  string  $otherKey
  * @param  string  $relation
  * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  */
 public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
 {
     // If no relation name was given, we will use this debug backtrace to extract
     // the calling method's name and use that as the relationship name as most
     // of the time this will be what we desire to use for the relatinoships.
     if (is_null($relation)) {
         list(, $caller) = debug_backtrace(false);
         $relation = $caller['function'];
     }
     // Check if it is a relation with an original model.
     if (!is_subclass_of($related, 'Jenssegers\\Mongodb\\Model')) {
         return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
     }
     // If no foreign key was supplied, we can use a backtrace to guess the proper
     // foreign key name by using the name of the relationship function, which
     // when combined with an "_id" should conventionally match the columns.
     if (is_null($foreignKey)) {
         $foreignKey = snake_case($relation) . '_id';
     }
     $instance = new $related();
     // Once we have the foreign key names, we'll just create a new Eloquent query
     // for the related models and returns the relationship instance which will
     // actually be responsible for retrieving and hydrating every relations.
     $query = $instance->newQuery();
     $otherKey = $otherKey ?: $instance->getKeyName();
     return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
 }
Exemplo n.º 5
0
 /**
  * an article comment belongs to an article comment child
  *
  * @class belongsTo parent::ArticleComment
  */
 public function children()
 {
     return parent::belongsTo(ArticleComment::class, 'parent_id', 'id');
 }