Skip to content

Commit

Permalink
Check for existence of Soft Deletes on certain methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Goodfield committed Feb 28, 2017
1 parent 7232b42 commit 552913d
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/PaulGoodfield/EloquentBaseRepo/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ public function all($columns = array('*'), $order = array())
*/
public function find($id, $columns = array('*'))
{
$model = $this->model->withTrashed()->find($id, $columns);
if ($this->usesSoftDeletes($this->model))
{
$model = $this->model->withTrashed();
}
else
{
$model = $this->model;
}

$model = $model->find($id, $columns);

if (is_null($model))
{
Expand Down Expand Up @@ -73,7 +82,32 @@ public function findWhere($fieldvals = array(), $columns = array('*'), $order =
$query->orderBy($key, $val);
}
}
return $this->convertCollection($query->withTrashed()->get());

if ($this->usesSoftDeletes($this->model))
{
$collection = $query->withTrashed()->get();
}
else
{
$collection = $query->get();
}

return $this->convertCollection($collection);
}

/**
* Determines if current model uses soft deletes
* @return boolean
*/
private function usesSoftDeletes($model)
{
$traits = class_uses($model);
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', $traits))
{
return true;
}

return false;
}

/**
Expand Down Expand Up @@ -193,8 +227,7 @@ public function convertModel($model)
$return = (object) $model->toArray();

// Check if model has soft deletes
$traits = class_uses($model);
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', $traits))
if ($this->usesSoftDeletes($model))
{
$return->trashed = ($model->trashed()) ? true : false;
}
Expand Down

0 comments on commit 552913d

Please sign in to comment.