Added support for self-referential many-to-many relationships in Eloquent.
This commit is contained in:
parent
b4c7dcb9ea
commit
17cc50375b
|
@ -264,9 +264,11 @@ public function belongs_to($model, $foreign_key = null)
|
|||
*
|
||||
* @param string $model
|
||||
* @param string $table
|
||||
* @param string $foreign_key
|
||||
* @param string $associated_key
|
||||
* @return mixed
|
||||
*/
|
||||
public function has_and_belongs_to_many($model, $table = null)
|
||||
public function has_and_belongs_to_many($model, $table = null, $foreign_key = null, $associated_key = null)
|
||||
{
|
||||
$this->relating = __FUNCTION__;
|
||||
|
||||
|
@ -283,11 +285,13 @@ public function has_and_belongs_to_many($model, $table = null)
|
|||
$this->relating_table = $table;
|
||||
}
|
||||
|
||||
$this->relating_key = strtolower(get_class($this)).'_id';
|
||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
|
||||
|
||||
$associated_key = (is_null($associated_key)) ? strtolower($model).'_id' : $associated_key;
|
||||
|
||||
return static::make($model)
|
||||
->select(array(static::table($model).'.*'))
|
||||
->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.strtolower($model).'_id')
|
||||
->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.$associated_key)
|
||||
->where($this->relating_table.'.'.$this->relating_key, '=', $this->id);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue