8000 Allow Model instance or id for first argument in Has_Many_And_Belongs_To::attach() by anaxamaxan · Pull Request #813 · laravel/laravel · GitHub
[go: up one dir, main page]

Skip to content

Allow Model instance or id for first argument in Has_Many_And_Belongs_To::attach() #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
0