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

morphOne() public method

Define a polymorphic one-to-one relationship.
public morphOne ( string $related, string $name, string $type = null, string $id = null, string $localKey = null ) : Illuminate\Database\Eloquent\Relations\MorphOne
$related string
$name string
$type string
$id string
$localKey string
return Illuminate\Database\Eloquent\Relations\MorphOne
    public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
    {
        $instance = new $related();
        if (!$instance->getConnectionName()) {
            $instance->setConnection($this->connection);
        }
        list($type, $id) = $this->getMorphs($name, $type, $id);
        $table = $instance->getTable();
        $localKey = $localKey ?: $this->getKeyName();
        return new MorphOne($instance->newQuery(), $this, $table . '.' . $type, $table . '.' . $id, $localKey);
    }

Usage Example

Example #1
0
 /**
  * Define a polymorphic one-to-one relationship.
  *
  * @param  string  $related
  * @param  string  $name
  * @param  string  $type
  * @param  string  $id
  * @param  string  $localKey
  * @return \Illuminate\Database\Eloquent\Relations\MorphOne
  */
 public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
 {
     // Check if it is a relation with an original model.
     if (!is_subclass_of($related, 'Jenssegers\\Mongodb\\Model')) {
         return parent::morphOne($related, $name, $type, $id, $localKey);
     }
     $instance = new $related();
     list($type, $id) = $this->getMorphs($name, $type, $id);
     $table = $instance->getTable();
     $localKey = $localKey ?: $this->getKeyName();
     return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
 }
Model