anonymous / gist:4dafa3379b1f27167269 secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Quick and dirty illustration of my comment to MKStandard on reddit. I know this could be improved - especially as the BaseRepository is assuming it uses Eloquent models (maybe an AbstractRepository that gets extended by an EloquentBaseRepository would be another logical layer of abstraction?) - but I think this should be enough of an illustration to get you on track. This hasn't been tested, and I don't know the details of any of your interfaces so the usual disclaimers and caveats apply.

View gist:4dafa3379b1f27167269
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<?php
class BaseRepository {
/**
* our model instance
*
* @var $model
*/
protected $model = null;
/**
* get our model
*
* @return $model
*/
protected function getModel()
{
return $this->model;
}
/**
* set our model
*
* @return void
*/
protected function setModel($model)
{
$this->model = $model;
}
public function get($id, array $related = null)
{
if(is_array($id))
{
$results = $this->model->whereIn("id", $id)->get();
}
else
{
$results = $this->model->findOrFail($id);
}
if( ! is_null($related))
{
$results->load($related);
}
return $results;
}
}
class EloquentMeasurementRepository extends BaseRepository implements MeasurementRepositoryInterface
{
public function __construct(Measurement $model)
{
$this->setModel($model);
}
}

You might also want to wrap the interior of the get function in an if (!is_null($this->model)){ ... } or similar to prevent trying to use null as a model. Like I said, quick and dirty.

Accessor or Mutators should be public or removed. In the constructor for EloquentMeasurementRepository you can set the model like this: $this->model = $model.

This also works but that's not what accessor and mutators are meant for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.