Illuminate\Database\Eloquent\Model::hasOne PHP Method

hasOne() public method

Define a one-to-one relationship.
public hasOne ( string $related, string $foreignKey = null, string $localKey = null ) : Illuminate\Database\Eloquent\Relations\HasOne
$related string
$foreignKey string
$localKey string
return Illuminate\Database\Eloquent\Relations\HasOne
    public function hasOne($related, $foreignKey = null, $localKey = null)
    {
        $foreignKey = $foreignKey ?: $this->getForeignKey();
        $instance = new $related();
        if (!$instance->getConnectionName()) {
            $instance->setConnection($this->connection);
        }
        $localKey = $localKey ?: $this->getKeyName();
        return new HasOne($instance->newQuery(), $this, $instance->getTable() . '.' . $foreignKey, $localKey);
    }

Usage Example

Example #1
0
 /**
  * Define a one-to-one relationship.
  *
  * @param  string  $related
  * @param  string  $foreignKey
  * @param  string  $localKey
  * @return \Illuminate\Database\Eloquent\Relations\HasOne
  */
 public function hasOne($related, $foreignKey = null, $localKey = null)
 {
     // Check if it is a relation with an original model.
     if (!is_subclass_of($related, 'Jenssegers\\Mongodb\\Model')) {
         return parent::hasOne($related, $foreignKey, $localKey);
     }
     $foreignKey = $foreignKey ?: $this->getForeignKey();
     $instance = new $related();
     $localKey = $localKey ?: $this->getKeyName();
     return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::hasOne
Model