Skip to content

Commit 852b10e

Browse files
committed
Merge pull request laravel#1143 from franzliedke/patch-38
Fix insert() method for related models.
2 parents db48fa4 + e46f07d commit 852b10e

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

laravel/database/eloquent/relationships/has_one_or_many.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ class Has_One_Or_Many extends Relationship {
77
/**
88
* Insert a new record for the association.
99
*
10+
* If save is successful, the model will be returned, otherwise false.
11+
*
1012
* @param Model|array $attributes
11-
* @return bool
13+
* @return Model|false
1214
*/
1315
public function insert($attributes)
1416
{
15-
$attributes = ($attributes instanceof Model) ? $attributes->attributes : $attributes;
16-
17-
$attributes[$this->foreign_key()] = $this->base->get_key();
17+
if ($attributes instanceof Model)
18+
{
19+
$attributes->set_attribute($this->foreign_key(), $this->base->get_key());
20+
21+
return $attributes->save() ? $attributes : false;
22+
}
23+
else
24+
{
25+
$attributes[$this->foreign_key()] = $this->base->get_key();
1826

19-
return $this->model->create($attributes);
27+
return $this->model->create($attributes);
28+
}
2029
}
2130

2231
/**

0 commit comments

Comments
 (0)