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:
kapil verma 2012-09-04 02:40:28 +05:30
parent ec13efb743
commit 55ad4e9a6d
1 changed files with 16 additions and 0 deletions

View File

@ -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;
}
}