belongsToMany() public method

Define a many-to-many relationship.
public belongsToMany ( string $related, string $table = null, string $foreignKey = null, string $otherKey = null, string $relation = null ) : Illuminate\Database\Eloquent\Relations\BelongsToMany
$related string
$table string
$foreignKey string
$otherKey string
$relation string
return Illuminate\Database\Eloquent\Relations\BelongsToMany
Example #1
0
 /**
  * For when you still want to use the 'normal' belongsToMany relationship in a CmsModel
  * that should be related to non-CmsModels in the laravel convention
  *
  * @param  string $related
  * @param  string $table
  * @param  string $foreignKey
  * @param  string $otherKey
  * @param  string $relation
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function belongsToManyNormal($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
 {
     return parent::belongsToMany($related, $table, $foreignKey, $otherKey, $relation);
 }
Example #2
0
 /**
  * Define a many-to-many relationship.
  *
  * @param  string  $related
  * @param  string  $collection
  * @param  string  $foreignKey
  * @param  string  $otherKey
  * @param  string  $relation
  * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  */
 public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
 {
     // If no relationship name was passed, we will pull backtraces to get the
     // name of the calling function. We will use that function name as the
     // title of this relation since that is a great convention to apply.
     if (is_null($relation)) {
         $relation = $this->getBelongsToManyCaller();
     }
     // Check if it is a relation with an original model.
     if (!is_subclass_of($related, 'Jenssegers\\Mongodb\\Model')) {
         return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
     }
     // First, we'll need to determine the foreign key and "other key" for the
     // relationship. Once we have determined the keys we'll make the query
     // instances as well as the relationship instances we need for this.
     $foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
     $instance = new $related();
     $otherKey = $otherKey ?: $instance->getForeignKey() . 's';
     // If no table name was provided, we can guess it by concatenating the two
     // models using underscores in alphabetical order. The two model names
     // are transformed to snake case from their default CamelCase also.
     if (is_null($collection)) {
         $collection = $instance->getTable();
     }
     // Now we're ready to create a new query builder for the related model and
     // the relationship instances for the relation. The relations will set
     // appropriate query constraint and entirely manages the hydrations.
     $query = $instance->newQuery();
     return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
 }
Example #3
0
 public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
 {
     $instance = with(new $class());
     $table = $instance->getTable();
     return parent::belongsToMany($class, SchemaLanguage::formatName($table), 'lang_id', SchemaLanguage::formatForeign($table))->withPivot($table->translatable());
 }