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

morphMany() public method

Define a polymorphic one-to-many relationship.
public morphMany ( string $related, string $name, string $type = null, string $id = null, string $localKey = null ) : Illuminate\Database\Eloquent\Relations\MorphMany
$related string
$name string
$type string
$id string
$localKey string
return Illuminate\Database\Eloquent\Relations\MorphMany
    public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
    {
        $instance = new $related();
        if (!$instance->getConnectionName()) {
            $instance->setConnection($this->connection);
        }
        // Here we will gather up the morph type and ID for the relationship so that we
        // can properly query the intermediate table of a relation. Finally, we will
        // get the table and create the relationship instances for the developers.
        list($type, $id) = $this->getMorphs($name, $type, $id);
        $table = $instance->getTable();
        $localKey = $localKey ?: $this->getKeyName();
        return new MorphMany($instance->newQuery(), $this, $table . '.' . $type, $table . '.' . $id, $localKey);
    }

Usage Example

Example #1
0
 /**
  * Define a polymorphic one-to-many relationship.
  *
  * @param  string  $related
  * @param  string  $name
  * @param  string  $type
  * @param  string  $id
  * @param  string  $localKey
  * @return \Illuminate\Database\Eloquent\Relations\MorphMany
  */
 public function morphMany($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::morphMany($related, $name, $type, $id, $localKey);
     }
     $instance = new $related();
     // Here we will gather up the morph type and ID for the relationship so that we
     // can properly query the intermediate table of a relation. Finally, we will
     // get the table and create the relationship instances for the developers.
     list($type, $id) = $this->getMorphs($name, $type, $id);
     $table = $instance->getTable();
     $localKey = $localKey ?: $this->getKeyName();
     return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
 }
Model