Skip to content

Commit

Permalink
Repository with relations
Browse files Browse the repository at this point in the history
  • Loading branch information
raylight75 committed Aug 23, 2016
1 parent e520ebf commit 0fa7452
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 186 deletions.
291 changes: 131 additions & 160 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function edit($id)
{
$data['title'] = $this->user->getTableName();
$data['roles'] = Role::all();
$data['user'] = $this->user->userWithFindbyId($id);
$data['user'] = $this->user->with('role')->findId($id);
return view('users.edit', $data);
}

Expand Down
7 changes: 7 additions & 0 deletions app/Repositories/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function findOrFail($value);
*/
public function whereIn($field, $value = array());

/**
* @param $field
* @param $value
* @return mixed
*/
public function with($relation);

/**
* @param $field
* @param $value
Expand Down
10 changes: 0 additions & 10 deletions app/Repositories/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public function getParents($parent)
->lists('product_id');
}

/**
* @param $id
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
*/
public function ItemProperty($id)
{
return $this->model->with('category', 'size', 'color')
->findOrFail($id);
}

/**
* @return mixed
*/
Expand Down
45 changes: 41 additions & 4 deletions app/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ abstract class Repository implements RepositoryInterface
*/
protected $model;

/**
* The relations to eager load.
*
* @var
*/
protected $with = [];

/**
* @param App $app
*
Expand Down Expand Up @@ -129,14 +136,34 @@ public function whereIn($attribute, $value = array())
}

/**
* @param array $relations
* Sets relations for eager loading.
*
* @param $relations
* @return $this
*/
/*public function with(array $relations)
public function with($relations)
{
$this->model = $this->model->with($relations);

if (is_string($relations))
{
$this->with = explode(',', $relations);

return $this;
}

$this->with = is_array($relations) ? $relations : [];

return $this;
}*/
}

/**
* @param $value
* @return mixed
*/
public function findId($value)
{
return $this->query()->findOrFail($value);
}

/**
* @param $id
Expand All @@ -155,4 +182,14 @@ public function makeModel()
$model = $this->app->make($this->model());
return $this->model = $model;
}

/**
* Creates a new QueryBuilder instance including eager loads
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function query ()
{
return $this->model->newQuery()->with($this->with);
}
}
9 changes: 0 additions & 9 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ public function userWithPaginate()
->paginate(10);
}

/**
* @param $id
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
*/
public function userWithFindbyId($id)
{
return $this->model->with('role')->findOrFail($id);
}

/**
* Users role
* @return \Illuminate\Database\Eloquent\Collection|static[]
Expand Down
4 changes: 2 additions & 2 deletions app/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function getProductInfo($slug, $id)
{
$data['latest'] = $this->product->latest();;
$data['products'] = $this->product->product();
$data['item'] = $this->product->ItemProperty($id);
$data['item'] = $this->product->with('category', 'size', 'color')->findId($id);
return $data;
}

Expand All @@ -137,7 +137,7 @@ public function getProductInfo($slug, $id)
*/
public function getFrameContent($id)
{
$data = $this->product->ItemProperty($id);
$data = $this->product->with('category', 'size', 'color')->findId($id);
return $data;
}

Expand Down

0 comments on commit 0fa7452

Please sign in to comment.