From fb9be0f0a3b01199b27ff853316b1be5ac98faba Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Fri, 20 Jul 2018 10:14:10 +0800 Subject: [PATCH] Change model query on Controller@index method --- src/stubs/controller.api.stub | 6 +++--- src/stubs/controller.full.stub | 6 +++--- src/stubs/controller.simple.stub | 6 +++--- tests/Generators/Api/ApiControllerGeneratorTest.php | 6 +++--- tests/Generators/FullControllerGeneratorTest.php | 18 +++++++++--------- .../Simple/SimpleControllerGeneratorTest.php | 18 +++++++++--------- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/stubs/controller.api.stub b/src/stubs/controller.api.stub index c5daf8a..fa868b8 100644 --- a/src/stubs/controller.api.stub +++ b/src/stubs/controller.api.stub @@ -15,9 +15,9 @@ class MastersController extends Controller */ public function index() { - $mstrCollections = Master::where(function ($query) { - $query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + $singleMstrQuery = Master::query(); + $singleMstrQuery->where('name', 'like', '%'.request('q').'%'); + $mstrCollections = $singleMstrQuery->paginate(25); return $mstrCollections; } diff --git a/src/stubs/controller.full.stub b/src/stubs/controller.full.stub index cfe5796..2651ebb 100644 --- a/src/stubs/controller.full.stub +++ b/src/stubs/controller.full.stub @@ -14,9 +14,9 @@ class MastersController extends Controller */ public function index() { - $mstrCollections = Master::where(function ($query) { - $query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + $singleMstrQuery = Master::query(); + $singleMstrQuery->where('name', 'like', '%'.request('q').'%'); + $mstrCollections = $singleMstrQuery->paginate(25); return view('masters.index', compact('mstrCollections')); } diff --git a/src/stubs/controller.simple.stub b/src/stubs/controller.simple.stub index 6e0a8b9..89ff9d7 100644 --- a/src/stubs/controller.simple.stub +++ b/src/stubs/controller.simple.stub @@ -15,9 +15,9 @@ class MastersController extends Controller public function index() { $editableMaster = null; - $mstrCollections = Master::where(function ($query) { - $query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + $singleMstrQuery = Master::query(); + $singleMstrQuery->where('name', 'like', '%'.request('q').'%'); + $mstrCollections = $singleMstrQuery->paginate(25); if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { $editableMaster = Master::find(request('id')); diff --git a/tests/Generators/Api/ApiControllerGeneratorTest.php b/tests/Generators/Api/ApiControllerGeneratorTest.php index f4c50e5..38bcd62 100644 --- a/tests/Generators/Api/ApiControllerGeneratorTest.php +++ b/tests/Generators/Api/ApiControllerGeneratorTest.php @@ -29,9 +29,9 @@ class {$this->plural_model_name}Controller extends Controller */ public function index() { - \${$this->collection_model_var_name} = {$this->model_name}::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \${$this->single_model_var_name}Query = {$this->model_name}::query(); + \${$this->single_model_var_name}Query->where('name', 'like', '%'.request('q').'%'); + \${$this->collection_model_var_name} = \${$this->single_model_var_name}Query->paginate(25); return \${$this->collection_model_var_name}; } diff --git a/tests/Generators/FullControllerGeneratorTest.php b/tests/Generators/FullControllerGeneratorTest.php index 27d5cd7..e16c391 100644 --- a/tests/Generators/FullControllerGeneratorTest.php +++ b/tests/Generators/FullControllerGeneratorTest.php @@ -28,9 +28,9 @@ class {$this->plural_model_name}Controller extends Controller */ public function index() { - \${$this->collection_model_var_name} = {$this->model_name}::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \${$this->single_model_var_name}Query = {$this->model_name}::query(); + \${$this->single_model_var_name}Query->where('name', 'like', '%'.request('q').'%'); + \${$this->collection_model_var_name} = \${$this->single_model_var_name}Query->paginate(25); return view('{$this->table_name}.index', compact('{$this->collection_model_var_name}')); } @@ -161,9 +161,9 @@ class CategoriesController extends Controller */ public function index() { - \$categories = Category::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \$categoryQuery = Category::query(); + \$categoryQuery->where('name', 'like', '%'.request('q').'%'); + \$categories = \$categoryQuery->paginate(25); return view('categories.index', compact('categories')); } @@ -295,9 +295,9 @@ class CategoriesController extends Controller */ public function index() { - \$categories = Category::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \$categoryQuery = Category::query(); + \$categoryQuery->where('name', 'like', '%'.request('q').'%'); + \$categories = \$categoryQuery->paginate(25); return view('categories.index', compact('categories')); } diff --git a/tests/Generators/Simple/SimpleControllerGeneratorTest.php b/tests/Generators/Simple/SimpleControllerGeneratorTest.php index 579603a..9a78def 100644 --- a/tests/Generators/Simple/SimpleControllerGeneratorTest.php +++ b/tests/Generators/Simple/SimpleControllerGeneratorTest.php @@ -29,9 +29,9 @@ class {$this->plural_model_name}Controller extends Controller public function index() { \$editable{$this->model_name} = null; - \${$this->collection_model_var_name} = {$this->model_name}::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \${$this->single_model_var_name}Query = {$this->model_name}::query(); + \${$this->single_model_var_name}Query->where('name', 'like', '%'.request('q').'%'); + \${$this->collection_model_var_name} = \${$this->single_model_var_name}Query->paginate(25); if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { \$editable{$this->model_name} = {$this->model_name}::find(request('id')); @@ -133,9 +133,9 @@ class CategoriesController extends Controller public function index() { \$editableCategory = null; - \$categories = Category::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \$categoryQuery = Category::query(); + \$categoryQuery->where('name', 'like', '%'.request('q').'%'); + \$categories = \$categoryQuery->paginate(25); if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { \$editableCategory = Category::find(request('id')); @@ -238,9 +238,9 @@ class CategoriesController extends Controller public function index() { \$editableCategory = null; - \$categories = Category::where(function (\$query) { - \$query->where('name', 'like', '%'.request('q').'%'); - })->paginate(25); + \$categoryQuery = Category::query(); + \$categoryQuery->where('name', 'like', '%'.request('q').'%'); + \$categories = \$categoryQuery->paginate(25); if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { \$editableCategory = Category::find(request('id'));