質問編集履歴

2

詳細2

2020/01/08 21:16

投稿

twinparadox
twinparadox

スコア42

test CHANGED
@@ -1 +1 @@
1
- [laravel]ソート機能を実装したい
1
+ [laravel]削除機能を追加したい
test CHANGED
@@ -36,6 +36,10 @@
36
36
 
37
37
 
38
38
 
39
+ ②を行う前に全テーブルを削除しmigrateを行いました。
40
+
41
+
42
+
39
43
  下記コードは②のコードになります。
40
44
 
41
45
  > Todo.php

1

詳細

2020/01/08 21:15

投稿

twinparadox
twinparadox

スコア42

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,277 @@
11
11
 
12
12
 
13
13
  [完成イメージ](https://gyazo.com/0dd4feeea3f7a27aefe6d2160944c65e)
14
+
15
+
16
+
17
+ 現在の実装ではidを全て消しても41〜となりオートインクリメントが働いている状態。
18
+
19
+
20
+
21
+ #やったこと
22
+
23
+ ①orderByの実装
24
+
25
+
26
+
27
+
28
+
29
+ ```
30
+
31
+ $items = DB::select('ALTER TABLE `tablename` auto_increment = 1');
32
+
33
+ ```
34
+
35
+ の実装
36
+
37
+
38
+
39
+ 下記コードは②のコードになります。
40
+
41
+ > Todo.php
42
+
43
+ ```
44
+
45
+ <?php
46
+
47
+
48
+
49
+ namespace App;
50
+
51
+
52
+
53
+ use Illuminate\Database\Eloquent\Model;
54
+
55
+
56
+
57
+ class Todo extends Model
58
+
59
+ {
60
+
61
+ protected $table = 'Todos';
62
+
63
+ protected $guarded = array('id');
64
+
65
+
66
+
67
+ public static $rules = array(
68
+
69
+ 'comment' => 'required'
70
+
71
+ );
72
+
73
+ }
74
+
75
+
76
+
77
+ ```
78
+
79
+ > TodoController.php
80
+
81
+ ```
82
+
83
+ <?php
84
+
85
+
86
+
87
+ namespace App\Http\Controllers;
88
+
89
+
90
+
91
+ use Illuminate\Http\Request;
92
+
93
+ use App\Http\Requests\CreateTaskRequest;
94
+
95
+ use Illuminate\Support\Facades\DB;
96
+
97
+ use App\Todo;
98
+
99
+
100
+
101
+ class TodoController extends Controller
102
+
103
+ {
104
+
105
+ public function index(Request $request)
106
+
107
+ {
108
+
109
+ $items = Todo::all();
110
+
111
+ return view('Todos.index', ['items' => $items]);
112
+
113
+ }
114
+
115
+
116
+
117
+ public function create(CreateTaskRequest $request)
118
+
119
+ {
120
+
121
+ $todo = new Todo;
122
+
123
+ $form = $request->all();
124
+
125
+ unset($form['_token']);
126
+
127
+ $todo->timestamps = false;
128
+
129
+ $todo->fill($form)->save();
130
+
131
+ $param = [
132
+
133
+ 'id' => $request->id,
134
+
135
+ 'comment' => $request->comment,
136
+
137
+ ];
138
+
139
+
140
+
141
+ return redirect('/Todo');
142
+
143
+ }
144
+
145
+ public function delete(Request $request)
146
+
147
+ {
148
+
149
+ Todo::find($request->table)->delete();
150
+
151
+ $items = DB::select('ALTER TABLE `tablename` auto_increment = 1');
152
+
153
+ return redirect('/Todo');
154
+
155
+ }
156
+
157
+ }
158
+
159
+
160
+
161
+ ```
162
+
163
+ > web.php
164
+
165
+ ```
166
+
167
+ <?php
168
+
169
+
170
+
171
+ /*
172
+
173
+ |--------------------------------------------------------------------------
174
+
175
+ | Web Routes
176
+
177
+ |--------------------------------------------------------------------------
178
+
179
+ |
180
+
181
+ | Here is where you can register web routes for your application. These
182
+
183
+ | routes are loaded by the RouteServiceProvider within a group which
184
+
185
+ | contains the "web" middleware group. Now create something great!
186
+
187
+ |
188
+
189
+ */
190
+
191
+
192
+
193
+ Route::get('/', function () {
194
+
195
+ return view('welcome');
196
+
197
+ });
198
+
199
+
200
+
201
+
202
+
203
+ Route::get('/Todo', 'TodoController@index');
204
+
205
+ Route::post('/Todo', 'TodoController@create');
206
+
207
+ Route::group(['middleware' => ['web']], function () {
208
+
209
+ Route::post('/Todo', 'TodoController@create');
210
+
211
+ });
212
+
213
+ Route::delete('/Todo/{table}', 'TodoController@delete');
214
+
215
+
216
+
217
+ ```
218
+
219
+ > view/Todos/index.blade.php
220
+
221
+ ```
222
+
223
+ @extends('layouts.index')
224
+
225
+ @section('title','Todoリスト')
226
+
227
+ @section('menubar')
228
+
229
+ @parent
230
+
231
+ Todoリスト
232
+
233
+ @endsection
234
+
235
+
236
+
237
+ @section('content')
238
+
239
+
240
+
241
+
242
+
243
+ <table>
244
+
245
+ <tr>
246
+
247
+ <th>ID</th>
248
+
249
+ <th>コメント</th>
250
+
251
+ <th>状態</th>
252
+
253
+ </tr>
254
+
255
+ @foreach ($items as $item)
256
+
257
+ <tr>
258
+
259
+ <td>{{$item->id}}</td>
260
+
261
+ <td>{{$item->comment}}</td>
262
+
263
+ <td>作業中</td>
264
+
265
+ <td>
266
+
267
+ <form action="{{ url('Todo/' . $item->id) }}" method="POST">
268
+
269
+ @csrf
270
+
271
+ @method('DELETE')
272
+
273
+ <button type="submit">削除</button>
274
+
275
+ </form>
276
+
277
+ </td>
278
+
279
+ </tr>
280
+
281
+ @endforeach
282
+
283
+ </table>
284
+
285
+ @endsection
286
+
287
+ ```