diff --git a/.idea/workspace.xml b/.idea/workspace.xml index daa4b88..334dbbd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,14 +5,13 @@ - - - - - - - + + + + + + @@ -50,14 +49,12 @@ - + - + - - - + @@ -65,28 +62,28 @@ - - + + - + - - + + - - + + - - + + @@ -114,7 +111,6 @@ @@ -309,7 +306,6 @@ - @@ -328,28 +324,15 @@ - - - - - - - - + - + @@ -452,14 +435,12 @@ - - - - 1456828439409 - 1456917466507 @@ -797,24 +778,31 @@ - - - + - + - + @@ -853,7 +841,8 @@ - @@ -870,41 +859,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1010,13 +964,6 @@ - - - - - - - @@ -1035,7 +982,6 @@ - @@ -1043,7 +989,6 @@ - @@ -1082,14 +1027,6 @@ - - - - - - - - @@ -1108,7 +1045,6 @@ - @@ -1119,13 +1055,6 @@ - - - - - - - @@ -1138,103 +1067,137 @@ - - - - + - - - + + - + - - + + - + + - - - - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + + + + + + + + - + - - + + - + - - - - - + + + - + - - + + - + - - + + - + - - + + - + - - + + - + @@ -1242,5 +1205,13 @@ + + + + + + + + \ No newline at end of file diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 4315516..9014f78 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -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); } diff --git a/app/Repositories/Contracts/RepositoryInterface.php b/app/Repositories/Contracts/RepositoryInterface.php index c97113a..ac13feb 100644 --- a/app/Repositories/Contracts/RepositoryInterface.php +++ b/app/Repositories/Contracts/RepositoryInterface.php @@ -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 diff --git a/app/Repositories/ProductRepository.php b/app/Repositories/ProductRepository.php index dd4a81e..3446190 100644 --- a/app/Repositories/ProductRepository.php +++ b/app/Repositories/ProductRepository.php @@ -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 */ diff --git a/app/Repositories/Repository.php b/app/Repositories/Repository.php index 4d1860a..50f529a 100644 --- a/app/Repositories/Repository.php +++ b/app/Repositories/Repository.php @@ -47,6 +47,13 @@ abstract class Repository implements RepositoryInterface */ protected $model; + /** + * The relations to eager load. + * + * @var + */ + protected $with = []; + /** * @param App $app * @@ -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 @@ -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); + } } \ No newline at end of file diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 4f7e175..401e398 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -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[] diff --git a/app/Services/BaseService.php b/app/Services/BaseService.php index 9d8ebb4..a8610b4 100644 --- a/app/Services/BaseService.php +++ b/app/Services/BaseService.php @@ -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; } @@ -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; }