質問編集履歴

2

誤字の修正

2019/06/18 15:01

投稿

neji-mawaso
neji-mawaso

スコア13

test CHANGED
File without changes
test CHANGED
@@ -50,7 +50,9 @@
50
50
 
51
51
 
52
52
 
53
- ```web.php
53
+ web.php
54
+
55
+ ```PHP
54
56
 
55
57
  <?php
56
58
 
@@ -130,7 +132,9 @@
130
132
 
131
133
 
132
134
 
133
- ```tasks.blade.php
135
+ tasks.blade.php
136
+
137
+ ```PHP
134
138
 
135
139
  @extends('layouts.app')
136
140
 
@@ -298,9 +302,9 @@
298
302
 
299
303
  ```
300
304
 
301
-
302
-
303
- ```ymd_create_tasks2_table.php
305
+ ymd_create_tasks2_table.php
306
+
307
+ ```PHP
304
308
 
305
309
  <?php
306
310
 

1

修正要請への対応

2019/06/18 15:01

投稿

neji-mawaso
neji-mawaso

スコア13

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,7 @@
24
24
 
25
25
 
26
26
 
27
- tasks2テーブルにアクセスするにはどうすれば良いでしょうか。
27
+ tasks2テーブルのデータを表示させるにはどうすれば良いでしょうか。
28
-
29
- テーブルに接続している部分はどこに当たるのでしょうか。
30
28
 
31
29
  回答お願いします。
32
30
 
@@ -48,4 +46,324 @@
48
46
 
49
47
  phpMyAdminでtasks2テーブルがあることは確認済みです。
50
48
 
51
- 変えた場所はマイグレーションファイルを作った時にテーブル名を変えたくらいなのでコードは載せていません。
49
+ ~~変えた場所はマイグレーションファイルを作った時にテーブル名を変えたくらいなのでコードは載せていません。~~載せます
50
+
51
+
52
+
53
+ ```web.php
54
+
55
+ <?php
56
+
57
+
58
+
59
+ use App\Task;
60
+
61
+ use Illuminate\Http\Request;
62
+
63
+
64
+
65
+ Route::get('/', function () {
66
+
67
+ $tasks = Task::orderBy('created_at','asc')->get();
68
+
69
+
70
+
71
+ return view('tasks',[
72
+
73
+ 'tasks' => $tasks
74
+
75
+ ]);
76
+
77
+ });
78
+
79
+
80
+
81
+ Route::post('/task',function (Request $request){
82
+
83
+ $validator = Validator::make($request->all(), [
84
+
85
+ 'name' => 'required|max:255',
86
+
87
+ ]);
88
+
89
+
90
+
91
+ if($validator->fails()){
92
+
93
+ return redirect('/')
94
+
95
+ ->withInput()
96
+
97
+ ->withErrors($validator);
98
+
99
+ }
100
+
101
+
102
+
103
+ $task = new Task;
104
+
105
+ $task->name = $request->name;
106
+
107
+ $task->save();
108
+
109
+
110
+
111
+ return redirect('/');
112
+
113
+
114
+
115
+ });
116
+
117
+
118
+
119
+ Route::delete('/task/{task}',function (Task $task){
120
+
121
+ $task->delete();
122
+
123
+
124
+
125
+ return redirect('/');
126
+
127
+ });
128
+
129
+ ```
130
+
131
+
132
+
133
+ ```tasks.blade.php
134
+
135
+ @extends('layouts.app')
136
+
137
+ @section('content')
138
+
139
+ <div class="container">
140
+
141
+ <div class="col-sm-offset-2 col-sm-8">
142
+
143
+ <div class="panel panel-default">
144
+
145
+ <div class="panel-heading">
146
+
147
+ New Task
148
+
149
+ </div>
150
+
151
+
152
+
153
+ <div class="panel-body">
154
+
155
+ <!-- Display Validation Errors -->
156
+
157
+ @include('common.errors')
158
+
159
+
160
+
161
+ <!-- New Task Form -->
162
+
163
+ <form action="{{ url('task') }}" method="POST" class="form-horizontal">
164
+
165
+ {{ csrf_field() }}
166
+
167
+
168
+
169
+ <!-- Task Name -->
170
+
171
+ <div class="form-group">
172
+
173
+ <label for="task-name" class="col-sm-3 control-label">Task</label>
174
+
175
+ <div class="col-sm-6">
176
+
177
+ <input type="text" name="name" id="task-name" class="form-control">
178
+
179
+ </div>
180
+
181
+ </div>
182
+
183
+
184
+
185
+ <!-- Add Task Button -->
186
+
187
+ <div class="form-group">
188
+
189
+ <div class="col-sm-offset-3 col-sm-6">
190
+
191
+ <button type="submit" class="btn btn-default">
192
+
193
+ <i class="fa fa-btn fa-plus"></i>Add Task
194
+
195
+ </button>
196
+
197
+ </div>
198
+
199
+ </div>
200
+
201
+ </form>
202
+
203
+ </div>
204
+
205
+ </div>
206
+
207
+
208
+
209
+ @if (count($tasks) > 0)
210
+
211
+ <div class="panel panel-default">
212
+
213
+ <div class="panel-heading">
214
+
215
+ Current Tasks
216
+
217
+ </div>
218
+
219
+
220
+
221
+ <div class="panel-body">
222
+
223
+ <table class="table table-striped task-table">
224
+
225
+ <thead>
226
+
227
+ <th>Task</th>
228
+
229
+ <th>&nbsp;</th>
230
+
231
+ </thead>
232
+
233
+ <tbody>
234
+
235
+ @foreach($tasks as $task)
236
+
237
+ <tr>
238
+
239
+ <td class="table-text"><div>{{ $task->name }}</div></td>
240
+
241
+ <tr>
242
+
243
+ <!-- Task Name -->
244
+
245
+ <td class="table-text">
246
+
247
+ <div>{{ $task->name }}</div>
248
+
249
+ </td>
250
+
251
+
252
+
253
+ <!-- Delete Button -->
254
+
255
+ <td>
256
+
257
+ <form action="{{ url('task/'.$task->id) }}" method="POST">
258
+
259
+ {{ csrf_field() }}
260
+
261
+ {{ method_field('DELETE') }}
262
+
263
+
264
+
265
+ <button type="submit" class="btn btn-danger">
266
+
267
+ <i class="fa fa-trash"></i> Delete
268
+
269
+ </button>
270
+
271
+ </form>
272
+
273
+ </td>
274
+
275
+ </tr>
276
+
277
+ <td>&nbsp;</td>
278
+
279
+ </tr>
280
+
281
+ @endforeach
282
+
283
+ </tbody>
284
+
285
+ </table>
286
+
287
+ </div>
288
+
289
+ </div>
290
+
291
+ @endif
292
+
293
+ </div>
294
+
295
+ </div>
296
+
297
+ @endsection
298
+
299
+ ```
300
+
301
+
302
+
303
+ ```ymd_create_tasks2_table.php
304
+
305
+ <?php
306
+
307
+
308
+
309
+ use Illuminate\Support\Facades\Schema;
310
+
311
+ use Illuminate\Database\Schema\Blueprint;
312
+
313
+ use Illuminate\Database\Migrations\Migration;
314
+
315
+
316
+
317
+ class CreateTasks2Table extends Migration
318
+
319
+ {
320
+
321
+ /**
322
+
323
+ * Run the migrations.
324
+
325
+ *
326
+
327
+ * @return void
328
+
329
+ */
330
+
331
+ public function up()
332
+
333
+ {
334
+
335
+ Schema::create('tasks2', function (Blueprint $table) {
336
+
337
+ $table->increments('id');
338
+
339
+ $table->string('name');
340
+
341
+ $table->timestamps();
342
+
343
+ });
344
+
345
+ }
346
+
347
+
348
+
349
+ /**
350
+
351
+ * Reverse the migrations.
352
+
353
+ *
354
+
355
+ * @return void
356
+
357
+ */
358
+
359
+ public function down()
360
+
361
+ {
362
+
363
+ Schema::dropIfExists('tasks2');
364
+
365
+ }
366
+
367
+ }
368
+
369
+ ```