回答編集履歴

1

追加

2018/03/09 06:44

投稿

yuuuui
yuuuui

スコア59

test CHANGED
@@ -7,3 +7,105 @@
7
7
  $users = DB::table('users')->paginate(15);
8
8
 
9
9
  ```
10
+
11
+
12
+
13
+ ↑コメントにも書きましたが、間違っていました。すみません。
14
+
15
+
16
+
17
+ **↓CostomPaginationはいかがでしょうか。**
18
+
19
+
20
+
21
+ ```
22
+
23
+ <?php
24
+
25
+
26
+
27
+
28
+
29
+ namespace App\Http\Controllers;
30
+
31
+
32
+
33
+ use Illuminate\Pagination\LengthAwarePaginator;
34
+
35
+ use Illuminate\Pagination\Paginator;
36
+
37
+
38
+
39
+ class CostomPaginationController extends Controller
40
+
41
+ {
42
+
43
+
44
+
45
+ public function index()
46
+
47
+ {
48
+
49
+ $array = [
50
+
51
+ (object)['name' => 'foo'],
52
+
53
+ (object)['name' => 'bar'],
54
+
55
+ (object)['name' => 'baz'],
56
+
57
+
58
+
59
+
60
+
61
+ ];
62
+
63
+ $perPage = 2;
64
+
65
+
66
+
67
+ //ページ番号
68
+
69
+ $page = max(0,Paginator::resolveCurrentPage() - 1);
70
+
71
+
72
+
73
+ //ページ内の要素を取得
74
+
75
+ $sliced = array_slice($array, $page * $perPage, $perPage);
76
+
77
+
78
+
79
+ //ページネータインスタンス生成
80
+
81
+ $paginator = new LengthAwarePaginator(
82
+
83
+ $sliced,
84
+
85
+ count($array),
86
+
87
+ $perPage,
88
+
89
+ null,
90
+
91
+ [
92
+
93
+ 'page' => $page,
94
+
95
+ 'path' => Paginator::resolveCurrentPath(),
96
+
97
+ ]
98
+
99
+ );
100
+
101
+
102
+
103
+ return view('user.index',['users' => $paginator]);
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ ```