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

hasMany() public method

Define a one-to-many relationship.
public hasMany ( string $related, string $foreignKey = null, string $localKey = null ) : Illuminate\Database\Eloquent\Relations\HasMany
$related string
$foreignKey string
$localKey string
return Illuminate\Database\Eloquent\Relations\HasMany
    public function hasMany($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 HasMany($instance->newQuery(), $this, $instance->getTable() . '.' . $foreignKey, $localKey);
    }

Usage Example

Example #1
0
 /**
  * Define a one-to-many relationship.
  *
  * @param  string  $related
  * @param  string  $foreignKey
  * @param  string  $localKey
  * @return \Illuminate\Database\Eloquent\Relations\HasMany
  */
 public function hasMany($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::hasMany($related, $foreignKey, $localKey);
     }
     $foreignKey = $foreignKey ?: $this->getForeignKey();
     $instance = new $related();
     $localKey = $localKey ?: $this->getKeyName();
     return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::hasMany
Model