8000 Merge pull request #813 from anaxamaxan/patch-3 · allforweb/laravel@46f68ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 46f68ab

Browse files
committed
Merge pull request laravel#813 from anaxamaxan/patch-3
Allow Model instance or id for first argument i 10000 n Has_Many_And_Belongs_To::attach()
2 parents 8f8fa09 + 19a3e9d commit 46f68ab

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

laravel/database/eloquent/relationships/has_many_and_belongs_to.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ public function results()
8585
/**
8686
* Insert a new record into the joining table of the association.
8787
*
88-
* @param int $id
89-
* @param array $joining
88+
* @param Model|int $id
89+
* @param array $attributes
9090
* @return bool
9191
*/
9292
public function attach($id, $attributes = array())
9393
{
94+
if ($id instanceof Model) $id = $id->get_key();
95+
9496
$joining = array_merge($this->join_record($id), $attributes);
9597

9698
return $this->insert_joining($joining);
@@ -99,12 +101,13 @@ public function attach($id, $attributes = array())
99101
/**
100102
* Detach a record from the joining table of the association.
101103
*
102-
* @param int $ids
104+
* @param array|Model|int $ids
103105
* @return bool
104106
*/
105107
public function detach($ids)
106108
{
107-
if ( ! is_array($ids)) $ids = array($ids);
109+
if ($ids instanceof Model) $ids = array($ids->get_key());
110+
elseif ( ! is_array($ids)) $ids = array($ids);
108111

109112
return $this->pivot()->where_in($this->other_key(), $ids)->delete();
110113
}

0 commit comments

Comments
 (0)
0