質問編集履歴
1
コントローラとビュー、ルーティングのコードを書き換えました、、二転三転してしまい申し訳ございません。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Undefined variable $user (View: C:\workspace\neomediaproject\resources\views\home.blade.php)
|
test
CHANGED
@@ -1,48 +1,118 @@
|
|
1
1
|
今ララベルを使ってユーザーの削除をユーザー自身で削除する機能を実装しているのですが、
|
2
2
|
|
3
|
+
Undefined variable $user (View: C:\workspace\neomediaproject\resources\views\home.blade.php) と出てしまい実装ができません。
|
4
|
+
|
3
5
|
|
4
6
|
|
5
|
-
|
7
|
+
|
6
|
-
|
7
|
-
ErrorException
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Array to string conversion
|
12
|
-
|
13
|
-
```
|
14
|
-
|
15
|
-
のエラーが出てしまいます。
|
16
|
-
|
17
|
-
ビューとルートとコントローラを利用しています。
|
18
|
-
|
19
|
-
原因はルーティングの
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
```php:ルーティングファイル
|
24
|
-
|
25
|
-
web.php
|
26
|
-
|
27
|
-
Route::resource('users', [Usercontroller::class, 'destroy']);
|
28
|
-
|
29
|
-
```
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
という事は理解できるのですが、ではこれが利用できないのであれば、ほかの実装方法などもあれば変えていきたいと思います。
|
34
8
|
|
35
9
|
念のため削除処理に必要な四つのファイルを載せます。
|
36
10
|
|
37
11
|
|
38
12
|
|
13
|
+
` home.blade.php (ユーザーがログインしたときに表示されるページ)`
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
@extends('layouts.app')
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
@section('content')
|
22
|
+
|
23
|
+
<div class="container">
|
24
|
+
|
25
|
+
<div class="row justify-content-center">
|
26
|
+
|
27
|
+
<div class="col-md-8">
|
28
|
+
|
29
|
+
<div class="card" class="col-sm-offset-2 col-sm-8">
|
30
|
+
|
31
|
+
<div class="card-header">{{ __('Dashboard') }}</div>
|
32
|
+
|
33
|
+
<div class="card-body">{{ Auth::user()->name }}</div>
|
34
|
+
|
35
|
+
<div class="card-body">{{ Auth::user()->email }}</div>
|
36
|
+
|
37
|
+
<div class="card-body">{{ Auth::user()->id }}</div>
|
38
|
+
|
39
|
+
<form action="/">
|
40
|
+
|
41
|
+
<button type="submit" class="btn btn-light">記事一覧へ</button>
|
42
|
+
|
43
|
+
</form>
|
44
|
+
|
45
|
+
<!-- ユーザー情報を削除ボタン押したらtopに戻る。補足:できればパスワードを要求するusercontrolerを使うか?homeからできるならやる -->
|
46
|
+
|
47
|
+
<!-- 削除ボタンを作る -->
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
<form action="/{{$user->id}}" method="POST">
|
54
|
+
|
55
|
+
@method('DELETE')
|
56
|
+
|
57
|
+
@csrf
|
58
|
+
|
59
|
+
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i>削除</button>
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
</form>
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
<div class="card-body">
|
68
|
+
|
69
|
+
@if (session('status'))
|
70
|
+
|
71
|
+
<div class="alert alert-success" role="alert">
|
72
|
+
|
73
|
+
{{ session('status') }}
|
74
|
+
|
75
|
+
</div>
|
76
|
+
|
77
|
+
@endif
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
{{ __('You are logged in!') }}
|
82
|
+
|
83
|
+
</div>
|
84
|
+
|
85
|
+
</div>
|
86
|
+
|
87
|
+
</div>
|
88
|
+
|
89
|
+
</div>
|
90
|
+
|
91
|
+
</div>
|
92
|
+
|
93
|
+
@endsection
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
39
107
|
---
|
40
108
|
|
41
109
|
|
42
110
|
|
43
|
-
`web.php`
|
111
|
+
`web.php ルーティング`
|
44
|
-
|
112
|
+
|
45
|
-
```
|
113
|
+
```
|
114
|
+
|
115
|
+
|
46
116
|
|
47
117
|
<?php
|
48
118
|
|
@@ -168,26 +238,22 @@
|
|
168
238
|
|
169
239
|
|
170
240
|
|
241
|
+
Route::delete('/{id}', 'UserController@destroy')->where('id', '[0-9]+');
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
171
|
-
|
247
|
+
Auth::routes();
|
248
|
+
|
172
|
-
|
249
|
+
/* 第一引数でhomeを読んだときそのページに移動してから第二引数ホームコントローラを呼びその中のindexクラスを使う
|
250
|
+
|
173
|
-
*
|
251
|
+
*indexクラスはyユーザーのビューを返す
|
252
|
+
|
253
|
+
*
|
174
254
|
|
175
255
|
*/
|
176
256
|
|
177
|
-
Route::resource('users', [Usercontroller::class, 'destroy']);
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
Auth::routes();
|
182
|
-
|
183
|
-
/* 第一引数でhomeを読んだとき第二引数ホームコントローラを呼びその中のindexクラスを使う
|
184
|
-
|
185
|
-
*indexクラスはyユーザーのビューを返す
|
186
|
-
|
187
|
-
*
|
188
|
-
|
189
|
-
*/
|
190
|
-
|
191
257
|
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
|
192
258
|
|
193
259
|
/* ユーザを削除するためのコントローラーとクラスを呼ぶ */
|
@@ -196,7 +262,11 @@
|
|
196
262
|
|
197
263
|
|
198
264
|
|
265
|
+
|
266
|
+
|
199
|
-
```
|
267
|
+
```
|
268
|
+
|
269
|
+
|
200
270
|
|
201
271
|
---
|
202
272
|
|
@@ -208,10 +278,6 @@
|
|
208
278
|
|
209
279
|
```
|
210
280
|
|
211
|
-
コントローラ:UserController.php
|
212
|
-
|
213
|
-
|
214
|
-
|
215
281
|
<?php
|
216
282
|
|
217
283
|
|
@@ -374,20 +440,18 @@
|
|
374
440
|
|
375
441
|
*/
|
376
442
|
|
377
|
-
public function destroy(
|
443
|
+
public function destroy($id)
|
378
|
-
|
444
|
+
|
379
|
-
{
|
445
|
+
{
|
380
|
-
|
446
|
+
|
381
|
-
/
|
447
|
+
//
|
382
|
-
|
383
|
-
|
448
|
+
|
384
|
-
|
385
|
-
*/
|
386
|
-
|
387
|
-
|
449
|
+
User::destroy($id);
|
388
450
|
|
389
451
|
return redirect('/');
|
390
452
|
|
453
|
+
|
454
|
+
|
391
455
|
}
|
392
456
|
|
393
457
|
}
|
@@ -408,87 +472,7 @@
|
|
408
472
|
|
409
473
|
|
410
474
|
|
411
|
-
|
475
|
+
|
412
|
-
|
413
|
-
```
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
@extends('layouts.app')
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
@section('content')
|
424
|
-
|
425
|
-
<div class="container">
|
426
|
-
|
427
|
-
<div class="row justify-content-center">
|
428
|
-
|
429
|
-
<div class="col-md-8">
|
430
|
-
|
431
|
-
<div class="card" class="col-sm-offset-2 col-sm-8">
|
432
|
-
|
433
|
-
<div class="card-header">{{ __('Dashboard') }}</div>
|
434
|
-
|
435
|
-
<div class="card-body">{{ Auth::user()->name }}</div>
|
436
|
-
|
437
|
-
<div class="card-body">{{ Auth::user()->email }}</div>
|
438
|
-
|
439
|
-
<form action="/">
|
440
|
-
|
441
|
-
<button type="submit" class="btn btn-light">記事一覧へ</button>
|
442
|
-
|
443
|
-
</form>
|
444
|
-
|
445
|
-
<!-- ユーザー情報を削除ボタン押したらtopに戻る。補足:できればパスワードを要求するusercontrolerを使うか?homeからできるならやる -->
|
446
|
-
|
447
|
-
<!-- 削除ボタンを作る -->
|
448
|
-
|
449
|
-
<form action="/" method="post">
|
450
|
-
|
451
|
-
@csrf
|
452
|
-
|
453
|
-
@method('DELETE')
|
454
|
-
|
455
|
-
<!-- {{ csrf_field() }} -->
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i>削除{{ __('Delete') }}</button>
|
460
|
-
|
461
|
-
</form>
|
462
|
-
|
463
|
-
<div class="card-body">
|
464
|
-
|
465
|
-
@if (session('status'))
|
466
|
-
|
467
|
-
<div class="alert alert-success" role="alert">
|
468
|
-
|
469
|
-
{{ session('status') }}
|
470
|
-
|
471
|
-
</div>
|
472
|
-
|
473
|
-
@endif
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
{{ __('You are logged in!') }}
|
478
|
-
|
479
|
-
</div>
|
480
|
-
|
481
|
-
</div>
|
482
|
-
|
483
|
-
</div>
|
484
|
-
|
485
|
-
</div>
|
486
|
-
|
487
|
-
</div>
|
488
|
-
|
489
|
-
@endsection
|
490
|
-
|
491
|
-
```
|
492
476
|
|
493
477
|
---
|
494
478
|
|
@@ -500,77 +484,79 @@
|
|
500
484
|
|
501
485
|
|
502
486
|
|
503
|
-
@
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
487
|
+
@<?php
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
use Illuminate\Database\Migrations\Migration;
|
492
|
+
|
493
|
+
use Illuminate\Database\Schema\Blueprint;
|
494
|
+
|
495
|
+
use Illuminate\Support\Facades\Schema;
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
class CreateUsersTable extends Migration
|
500
|
+
|
501
|
+
{
|
502
|
+
|
503
|
+
/**
|
504
|
+
|
505
|
+
* Run the migrations.
|
506
|
+
|
507
|
+
*
|
508
|
+
|
509
|
+
* @return void
|
510
|
+
|
511
|
+
*/
|
512
|
+
|
513
|
+
public function up()
|
514
|
+
|
515
|
+
{
|
516
|
+
|
517
|
+
Schema::create('users', function (Blueprint $table) {
|
518
|
+
|
519
|
+
$table->id();
|
520
|
+
|
521
|
+
$table->string('name');
|
522
|
+
|
523
|
+
$table->string('email')->unique();
|
524
|
+
|
525
|
+
$table->timestamp('email_verified_at')->nullable();
|
526
|
+
|
527
|
+
$table->string('password');
|
528
|
+
|
529
|
+
$table->rememberToken();
|
530
|
+
|
531
|
+
$table->timestamps();
|
532
|
+
|
533
|
+
});
|
534
|
+
|
535
|
+
}
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
/**
|
540
|
+
|
541
|
+
* Reverse the migrations.
|
542
|
+
|
543
|
+
*
|
544
|
+
|
545
|
+
* @return void
|
546
|
+
|
547
|
+
*/
|
548
|
+
|
549
|
+
public function down()
|
550
|
+
|
551
|
+
{
|
552
|
+
|
553
|
+
Schema::dropIfExists('users');
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
|
574
560
|
|
575
561
|
|
576
562
|
|