diff --git a/README.md b/README.md index 9f22ac78..95a62545 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ php artisan vendor:publish - delete($id) - orderBy($column, $direction = 'asc'); - with(array $relations); +- has(string $relation); +- whereHas(string $relation, closure $closure); - hidden(array $fields); - visible(array $fields); - scopeQuery(Closure $scope); @@ -301,6 +303,12 @@ public function __construct({YOUR_NAMESPACE}Repositories\PostRepository $reposit } ``` +Alternatively, you could use the artisan command to do the binding for you. + +```php +php artisan make:bindings Cats +``` + ### Use methods ```php diff --git a/src/Prettus/Repository/Criteria/RequestCriteria.php b/src/Prettus/Repository/Criteria/RequestCriteria.php index 51b44678..1d187654 100644 --- a/src/Prettus/Repository/Criteria/RequestCriteria.php +++ b/src/Prettus/Repository/Criteria/RequestCriteria.php @@ -68,10 +68,10 @@ public function apply($model, RepositoryInterface $repository) $condition = trim(strtolower($condition)); if (isset($searchData[$field])) { - $value = $condition == "like" ? "%{$searchData[$field]}%" : $searchData[$field]; + $value = ($condition == "like" || $condition == "ilike") ? "%{$searchData[$field]}%" : $searchData[$field]; } else { if (!is_null($search)) { - $value = $condition == "like" ? "%{$search}%" : $search; + $value = ($condition == "like" || $condition == "ilike") ? "%{$search}%" : $search; } } diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index de2837ed..68206e14 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -262,7 +262,9 @@ public function scopeQuery(\Closure $scope) */ public function lists($column, $key = null) { - return $this->makeModel()->lists($column, $key); + $this->applyCriteria(); + + return $this->model->lists($column, $key); } /** @@ -393,14 +395,7 @@ public function findWhere(array $where, $columns = ['*']) $this->applyCriteria(); $this->applyScope(); - foreach ($where as $field => $value) { - if (is_array($value)) { - list($field, $condition, $val) = $value; - $this->model = $this->model->where($field, $condition, $val); - } else { - $this->model = $this->model->where($field, '=', $value); - } - } + $this->applyConditions($where); $model = $this->model->get($columns); $this->resetModel(); @@ -571,6 +566,32 @@ public function delete($id) return $deleted; } + /** + * Delete multiple entities by given criteria. + * + * @param array $where + * + * @return int + */ + public function deleteWhere(array $where) + { + $this->applyScope(); + + $temporarySkipPresenter = $this->skipPresenter; + $this->skipPresenter(true); + + $this->applyConditions($where); + + $deleted = $this->model->delete(); + + event(new RepositoryEntityDeleted($this, $this->model)); + + $this->skipPresenter($temporarySkipPresenter); + $this->resetModel(); + + return $deleted; + } + /** * Check if entity has relation * @@ -598,6 +619,21 @@ public function with($relations) return $this; } + + /** + * Load relation with closure + * + * @param string $relation + * @param closure $closure + * + * @return $this + */ + function whereHas($relation, $closure) + { + $this->model = $this->model->whereHas($relation, $closure); + + return $this; + } /** * Set hidden fields @@ -783,6 +819,24 @@ protected function applyCriteria() return $this; } + /** + * Applies the given where conditions to the model. + * + * @param array $where + * @return void + */ + protected function applyConditions(array $where) + { + foreach ($where as $field => $value) { + if (is_array($value)) { + list($field, $condition, $val) = $value; + $this->model = $this->model->where($field, $condition, $val); + } else { + $this->model = $this->model->where($field, '=', $value); + } + } + } + /** * Skip Presenter Wrapper * diff --git a/src/Prettus/Repository/Generators/ControllerGenerator.php b/src/Prettus/Repository/Generators/ControllerGenerator.php index 96c576eb..4a34f2d2 100644 --- a/src/Prettus/Repository/Generators/ControllerGenerator.php +++ b/src/Prettus/Repository/Generators/ControllerGenerator.php @@ -91,6 +91,7 @@ public function getReplacements() 'singular' => $this->getSingularName(), 'validator' => $this->getValidator(), 'repository' => $this->getRepository(), + 'manager' => $this->getManager(), 'appname' => $this->getAppNamespace(), ]); } @@ -105,7 +106,24 @@ public function getSingularName() return str_singular(lcfirst(ucwords($this->getClass()))); } + /** + * Gets manager full class name + * + * @return string + */ + public function getManager() + { + $managerGenerator = new ManagerGenerator([ + 'name' => $this->name, + ]); + + $manager = $managerGenerator->getRootNamespace() . '\\' . $managerGenerator->getName(); + return 'use ' . str_replace([ + "\\", + '/' + ], '\\', $manager) . 'Manager;'; + } /** * Gets validator full class name * diff --git a/src/Prettus/Repository/Generators/Generator.php b/src/Prettus/Repository/Generators/Generator.php index 9a120965..ec6c9968 100644 --- a/src/Prettus/Repository/Generators/Generator.php +++ b/src/Prettus/Repository/Generators/Generator.php @@ -78,7 +78,13 @@ public function setFilesystem(Filesystem $filesystem) */ public function getStub() { - return (new Stub(__DIR__ . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render(); + $path = config('repository.generator.stubsOverridePath', __DIR__); + + if(!file_exists($path . '/Stubs/' . $this->stub . '.stub')){ + $path = __DIR__; + } + + return (new Stub($path . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render(); } diff --git a/src/Prettus/Repository/Generators/MigrationGenerator.php b/src/Prettus/Repository/Generators/MigrationGenerator.php index 32e0dfc5..bbb23e6b 100644 --- a/src/Prettus/Repository/Generators/MigrationGenerator.php +++ b/src/Prettus/Repository/Generators/MigrationGenerator.php @@ -153,11 +153,16 @@ public function getStub() ]; break; } + $path = config('repository.generator.stubsOverridePath', __DIR__); - if (!file_exists(__DIR__ . "/Stubs/migration/{$file}.stub")) { - throw new FileNotFoundException(__DIR__ . "/Stubs/migration/{$file}.stub"); + if (!file_exists($path . "/Stubs/migration/{$file}.stub")) { + $path = __DIR__; } - return Stub::create(__DIR__ . "/Stubs/migration/{$file}.stub", $replacements); + if (!file_exists($path . "/Stubs/migration/{$file}.stub")) { + throw new FileNotFoundException($path . "/Stubs/migration/{$file}.stub"); + } + + return Stub::create($path . "/Stubs/migration/{$file}.stub", $replacements); } } diff --git a/src/Prettus/Repository/Generators/Stubs/controller/controller.stub b/src/Prettus/Repository/Generators/Stubs/controller/controller.stub index 0e6d1500..d82f8a73 100644 --- a/src/Prettus/Repository/Generators/Stubs/controller/controller.stub +++ b/src/Prettus/Repository/Generators/Stubs/controller/controller.stub @@ -9,8 +9,10 @@ use Prettus\Validator\Contracts\ValidatorInterface; use Prettus\Validator\Exceptions\ValidatorException; use $APPNAME$Http\Requests\$CLASS$CreateRequest; use $APPNAME$Http\Requests\$CLASS$UpdateRequest; +use $APPNAME$Http\Requests\$CLASS$UpdateRequest; $REPOSITORY$ $VALIDATOR$ +$MANAGER$ class $CONTROLLER$Controller extends Controller @@ -25,12 +27,16 @@ class $CONTROLLER$Controller extends Controller * @var $CLASS$Validator */ protected $validator; + /** + * @var $CLASS$Manager + */ + protected $manager; - - public function __construct($CLASS$Repository $repository, $CLASS$Validator $validator) + public function __construct($CLASS$Repository $repository, $CLASS$Validator $validatorm, $CLASS$Manager) { $this->repository = $repository; $this->validator = $validator; + $this->manager = $manager; } @@ -41,7 +47,6 @@ class $CONTROLLER$Controller extends Controller */ public function index() { - $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria')); $$PLURAL$ = $this->repository->all(); @@ -55,19 +60,6 @@ class $CONTROLLER$Controller extends Controller return view('$PLURAL$.index', compact('$PLURAL$')); } - - /** - * Show the form for creating a new resource. - * - * @return \Illuminate\Http\Response - */ - public function create() - { - - return view('$PLURAL$.create'); - } - - /** * Store a newly created resource in storage. * @@ -82,7 +74,7 @@ class $CONTROLLER$Controller extends Controller $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE); - $$SINGULAR$ = $this->repository->create($request->all()); + $$SINGULAR$ = $this->manager->create($request->all()); $response = [ 'message' => '$CLASS$ created.', @@ -161,7 +153,7 @@ class $CONTROLLER$Controller extends Controller $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE); - $$SINGULAR$ = $this->repository->update($request->all(), $id); + $$SINGULAR$ = $this->manager->update($id, $request->all()); $response = [ 'message' => '$CLASS$ updated.', @@ -198,7 +190,7 @@ class $CONTROLLER$Controller extends Controller */ public function destroy($id) { - $deleted = $this->repository->delete($id); + $deleted = $this->manager->delete($id); if (request()->wantsJson()) { diff --git a/src/Prettus/Repository/Generators/Stubs/validator/validator.stub b/src/Prettus/Repository/Generators/Stubs/validator/validator.stub index bc08826e..b32c3fdc 100644 --- a/src/Prettus/Repository/Generators/Stubs/validator/validator.stub +++ b/src/Prettus/Repository/Generators/Stubs/validator/validator.stub @@ -5,11 +5,11 @@ $NAMESPACE$ use \Prettus\Validator\Contracts\ValidatorInterface; use \Prettus\Validator\LaravelValidator; -class $CLASS$Validator extends LaravelValidator { +class $CLASS$Validator extends LaravelValidator +{ protected $rules = [ ValidatorInterface::RULE_CREATE => $RULES$, ValidatorInterface::RULE_UPDATE => $RULES$, ]; - -} \ No newline at end of file +} diff --git a/src/resources/config/repository.php b/src/resources/config/repository.php index e4f1d7c4..2aca7cdb 100644 --- a/src/resources/config/repository.php +++ b/src/resources/config/repository.php @@ -231,6 +231,7 @@ 'controllers' => 'Http/Controllers', 'provider' => 'RepositoryServiceProvider', 'criteria' => 'Criteria', + 'stubsOverridePath' => app_path() ] ] ];