Skip to content

Commit

Permalink
Merge pull request #358 from joaosalless/master
Browse files Browse the repository at this point in the history
Add firstOrCreate method
  • Loading branch information
eullercdr authored Mar 15, 2017
2 parents f509a51 + ca84a3e commit 12aadec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Prettus/Repository/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ public function skipPresenter($status = true);
* @return mixed
*/
public function firstOrNew(array $attributes = []);

/**
* Retrieve first data of repository, or create new Entity
*
* @param array $attributes
*
* @return mixed
*/
public function firstOrCreate(array $attributes = []);
}
23 changes: 23 additions & 0 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,29 @@ public function firstOrNew(array $attributes = [])
return $this->parserResult($model);
}

/**
* Retrieve first data of repository, or create new Entity
*
* @param array $attributes
*
* @return mixed
*/
public function firstOrCreate(array $attributes = [])
{
$this->applyCriteria();
$this->applyScope();

$temporarySkipPresenter = $this->skipPresenter;
$this->skipPresenter(true);

$model = $this->model->firstOrCreate($attributes);
$this->skipPresenter($temporarySkipPresenter);

$this->resetModel();

return $this->parserResult($model);
}

/**
* Retrieve all data of repository, paginated
*
Expand Down

0 comments on commit 12aadec

Please sign in to comment.