Skip to content

Commit

Permalink
add filtering to request
Browse files Browse the repository at this point in the history
  • Loading branch information
Mdhesari committed Jan 30, 2022
1 parent ef82436 commit f7a43c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Http/Controllers/CityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class CityController extends Controller
* @return \Illuminate\Http\JsonResponse
* @group Country-Cities
*/
public function index(State $state): \Illuminate\Http\JsonResponse
public function index(Request $request, State $state): \Illuminate\Http\JsonResponse
{
$query = $state->cities()->query();

if ($request->has('s')) {
$query->where('name', 'like', '*' . $request->get('s') . '*');
}

return api()->success(null, [
'item' => $state->cities()->get(),
'items' => $query->paginate($request->get('per_page', 16)),
]);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Http/Controllers/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ class CountryController extends Controller
* @return \Illuminate\Http\JsonResponse
* @group Country-Cities
*/
public function index(): \Illuminate\Http\JsonResponse
public function index(Request $request): \Illuminate\Http\JsonResponse
{
$query = Country::query();

if ($request->has('s')) {
$query->where('name', 'like', '*' . $request->get('s') . '*');
}

return api()->success(null, [
'items' => Country::paginate(),
'items' => $query->paginate($request->get('per_page', 16)),
]);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Http/Controllers/StateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mdhesari\LaravelCountryStateCities\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Mdhesari\LaravelCountryStateCities\Models\Country;
use Mdhesari\LaravelCountryStateCities\Models\State;
Expand All @@ -15,10 +16,16 @@ class StateController extends Controller
* @return \Illuminate\Http\JsonResponse
* @group Country-Cities
*/
public function index(Country $country): \Illuminate\Http\JsonResponse
public function index(Request $request, Country $country): \Illuminate\Http\JsonResponse
{
$query = $country->states()->query();

if ($request->has('s')) {
$query->where('name', 'like', '*' . $request->get('s') . '*');
}

return api()->success(null, [
'item' => $country->states()->get(),
'items' => $query->paginate($request->get('per_page', 16)),
]);
}

Expand Down

0 comments on commit f7a43c1

Please sign in to comment.