Skip to content

Commit dd46da2

Browse files
committed
Add search feature for table in dashboard
1 parent 7ec1b28 commit dd46da2

18 files changed

+289
-60
lines changed

app/Http/Controllers/Api/ArticleController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Api;
44

5+
use Illuminate\Http\Request;
56
use App\Http\Requests\ArticleRequest;
67
use App\Repositories\ArticleRepository;
78

@@ -21,9 +22,9 @@ public function __construct(ArticleRepository $article)
2122
*
2223
* @return \Illuminate\Http\JsonResponse
2324
*/
24-
public function index()
25+
public function index(Request $request)
2526
{
26-
return $this->response->collection($this->article->page());
27+
return $this->response->collection($this->article->pageWithRequest($request));
2728
}
2829

2930
/**

app/Http/Controllers/Api/CategoryController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function __construct(CategoryRepository $category)
2222
*
2323
* @return \Illuminate\Http\JsonResponse
2424
*/
25-
public function index()
25+
public function index(Request $request)
2626
{
27-
return $this->response->collection($this->category->page());
27+
return $this->response->collection($this->category->pageWithRequest($request));
2828
}
2929

3030
/**
@@ -99,7 +99,7 @@ public function update(CategoryRequest $request, $id)
9999
* Remove the specified resource from storage.
100100
*
101101
* @param int $id
102-
*
102+
*
103103
* @return \Illuminate\Http\JsonResponse
104104
*/
105105
public function destroy($id)

app/Http/Controllers/Api/CommentController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function __construct(CommentRepository $comment)
2424
*
2525
* @return \Illuminate\Http\JsonResponse
2626
*/
27-
public function index()
27+
public function index(Request $request)
2828
{
29-
return $this->response->collection($this->comment->page());
29+
return $this->response->collection($this->comment->pageWithRequest($request));
3030
}
3131

3232
/**

app/Http/Controllers/Api/DiscussionController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function __construct(DiscussionRepository $discussion)
2222
*
2323
* @return \Illuminate\Http\JsonResponse
2424
*/
25-
public function index()
25+
public function index(Request $request)
2626
{
27-
return $this->response->collection($this->discussion->page(10, 'desc'));
27+
return $this->response->collection($this->discussion->pageWithRequest($request));
2828
}
2929

3030
/**

app/Http/Controllers/Api/LinkController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public function __construct(LinkRepository $link)
2020

2121
$this->manager = app('uploader');
2222
}
23-
23+
2424
/**
2525
* Display a listing of the resource.
2626
*
2727
* @return \Illuminate\Http\JsonResponse
2828
*/
29-
public function index()
29+
public function index(Request $request)
3030
{
31-
return $this->response->collection($this->link->page());
31+
return $this->response->collection($this->link->pageWithRequest($request));
3232
}
3333

3434
/**

app/Http/Controllers/Api/TagController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function __construct(TagRepository $tag)
2222
*
2323
* @return \Illuminate\Http\JsonResponse
2424
*/
25-
public function index()
25+
public function index(Request $request)
2626
{
27-
return $this->response->collection($this->tag->page());
27+
return $this->response->collection($this->tag->pageWithRequest($request));
2828
}
2929

3030
/**

app/Http/Controllers/Api/UserController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function __construct(UserRepository $user)
2323
*
2424
* @return \Illuminate\Http\JsonResponse
2525
*/
26-
public function index()
26+
public function index(Request $request)
2727
{
28-
return $this->response->collection($this->user->page());
28+
return $this->response->collection($this->user->pageWithRequest($request));
2929
}
3030

3131
/**
@@ -93,7 +93,7 @@ public function update(Request $request, $id)
9393

9494
/**
9595
* Crop Avatar
96-
*
96+
*
9797
* @param Request $request
9898
* @return \Illuminate\Http\JsonResponse
9999
*/

app/Http/Controllers/Api/VisitorController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Api;
44

5+
use Illuminate\Http\Request;
56
use App\Repositories\VisitorRepository;
67

78
class VisitorController extends ApiController
@@ -20,9 +21,9 @@ public function __construct(VisitorRepository $visitor)
2021
*
2122
* @return \Illuminate\Http\JsonResponse
2223
*/
23-
public function index()
24+
public function index(Request $request)
2425
{
25-
return $this->response->collection($this->visitor->page());
26+
return $this->response->collection($this->visitor->pageWithRequest($request));
2627
}
2728

2829
}

app/Repositories/ArticleRepository.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,30 @@ public function __construct(Article $article, VisitorRepository $visitor)
2222

2323
/**
2424
* Get the page of articles without draft scope.
25-
*
25+
*
26+
* @param Request $request
27+
* @param integer $number
28+
* @param string $sort
29+
* @param string $sortColumn
30+
* @return collection
31+
*/
32+
public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at')
33+
{
34+
$this->model = $this->checkAuthScope();
35+
36+
$keyword = $request->get('keyword');
37+
38+
return $this->model
39+
->when($keyword, function ($query) use ($keyword) {
40+
$query->where('title', 'like', "%{$keyword}%")
41+
->orWhere('subtitle', 'like', "%{$keyword}%");
42+
})
43+
->orderBy($sortColumn, $sort)->paginate($number);
44+
}
45+
46+
/**
47+
* Get the page of articles without draft scope.
48+
*
2649
* @param integer $number
2750
* @param string $sort
2851
* @param string $sortColumn
@@ -37,7 +60,7 @@ public function page($number = 10, $sort = 'desc', $sortColumn = 'created_at')
3760

3861
/**
3962
* Get the article record without draft scope.
40-
*
63+
*
4164
* @param int $id
4265
* @return mixed
4366
*/
@@ -48,7 +71,7 @@ public function getById($id)
4871

4972
/**
5073
* Update the article record without draft scope.
51-
*
74+
*
5275
* @param int $id
5376
* @param array $input
5477
* @return boolean
@@ -82,7 +105,7 @@ public function getBySlug($slug)
82105

83106
/**
84107
* Check the auth and the model without global scope when user is the admin.
85-
*
108+
*
86109
* @return Model
87110
*/
88111
public function checkAuthScope()
@@ -107,7 +130,7 @@ public function syncTag(array $tags)
107130

108131
/**
109132
* Search the articles by the keyword.
110-
*
133+
*
111134
* @param string $key
112135
* @return collection
113136
*/

app/Repositories/CategoryRepository.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,29 @@ public function __construct(Category $category)
1515
$this->model = $category;
1616
}
1717

18+
/**
19+
* Get number of the records.
20+
*
21+
* @param Request $request
22+
* @param integer $number
23+
* @param string $sort
24+
* @param string $sortColumn
25+
* @return collection
26+
*/
27+
public function pageWithRequest($request, $number = 10, $sort = 'desc', $sortColumn = 'created_at')
28+
{
29+
$keyword = $request->get('keyword');
30+
31+
return $this->model
32+
->when($keyword, function ($query) use ($keyword) {
33+
$query->where('name', 'like', "%{$keyword}%");
34+
})
35+
->orderBy($sortColumn, $sort)->paginate($number);
36+
}
37+
1838
/**
1939
* Get record by the name.
20-
*
40+
*
2141
* @param string $name
2242
* @return collection
2343
*/

0 commit comments

Comments
 (0)