From 19a3e9dc2b42de476aade6a75d8b457d4a93a425 Mon Sep 17 00:00:00 2001 From: anaxamaxan Date: Wed, 13 Jun 2012 16:23:28 -0700 Subject: [PATCH] Allow Model instance an id for first argument in Has_Many_And_Belongs_To::attach() and same for ::detach(). Also a typo fix for the docblock on attach(). --- .../relationships/has_many_and_belongs_to.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php index 587e504cdd9..cc921861b02 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -85,12 +85,14 @@ public function results() /** * Insert a new record into the joining table of the association. * - * @param int $id - * @param array $joining + * @param Model|int $id + * @param array $attributes * @return bool */ public function attach($id, $attributes = array()) { + if ($id instanceof Model) $id = $id->get_key(); + $joining = array_merge($this->join_record($id), $attributes); return $this->insert_joining($joining); @@ -99,12 +101,13 @@ public function attach($id, $attributes = array()) /** * Detach a record from the joining table of the association. * - * @param int $ids + * @param array|Model|int $ids * @return bool */ public function detach($ids) { - if ( ! is_array($ids)) $ids = array($ids); + if ($ids instanceof Model) $ids = array($ids->get_key()); + elseif ( ! is_array($ids)) $ids = array($ids); return $this->pivot()->where_in($this->other_key(), $ids)->delete(); }