Permalink
Browse files

Added firstOrFail to belongsToMany.

  • Loading branch information...
taylorotwell committed Nov 1, 2013
1 parent 710440c commit b162579b3911f59f5ca175d2d90d0a221cf6b109
Showing with 15 additions and 1 deletion.
  1. +15 −1 src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
@@ -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 {
@@ -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.
*
@@ -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();

0 comments on commit b162579

Please sign in to comment.