Added a method to fluently set belongs-to relation
This method spun off from a discussion in IRC. What this enables is this: $user->comments()->insert($comment_data)->article()->bind($article->id)
This commit is contained in:
parent
ec13efb743
commit
55ad4e9a6d
|
@ -110,5 +110,21 @@ public function foreign_value()
|
|||
{
|
||||
return $this->base->get_attribute($this->foreign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind an object over a belongs-to relation using its id.
|
||||
*
|
||||
* @return Eloquent
|
||||
*/
|
||||
|
||||
public function bind($id)
|
||||
{
|
||||
if((int) $this->foreign_value() === (int) $id)
|
||||
return $this->base;
|
||||
|
||||
$this->base->fill(array($this->foreign => $id))->save();
|
||||
|
||||
return $this->base;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue