Skip to content

Commit

Permalink
Added firstOrFail to belongsToMany.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 1, 2013
1 parent 710440c commit b162579
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class BelongsToMany extends Relation {

Expand Down Expand Up @@ -87,6 +88,19 @@ public function first($columns = array('*'))
return count($results) > 0 ? $results->first() : null;
}

/**
* Execute the query and get the first result or throw an exception.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|static
*/
public function firstOrFail($columns = array('*'))
{
if ( ! is_null($model = $this->first($columns))) return $model;

throw new ModelNotFoundException;
}

/**
* Execute the query as a "select" statement.
*
Expand Down Expand Up @@ -742,7 +756,7 @@ public function detach($ids = array(), $touch = true)
* @return void
*/
public function touchIfTouching()
{
{
if ($this->touchingParent()) $this->getParent()->touch();

if ($this->getParent()->touches($this->relationName)) $this->touch();
Expand Down

0 comments on commit b162579

Please sign in to comment.