質問編集履歴

4

コードを編集しました。

2019/12/26 13:52

投稿

GO999
GO999

スコア5

test CHANGED
File without changes
test CHANGED
@@ -266,7 +266,7 @@
266
266
 
267
267
  use App\Post;
268
268
 
269
-
269
+ use App\Http\Controllers\Controller;
270
270
 
271
271
  class PostsController extends Controller
272
272
 
@@ -290,8 +290,6 @@
290
290
 
291
291
  $posts = Post::with(['comments'])->orderBy('created_at', 'desc')->paginate(10);
292
292
 
293
-
294
-
295
293
  return view('posts.bbs',['posts'=>$posts]);
296
294
 
297
295
  }
@@ -322,7 +320,9 @@
322
320
 
323
321
 
324
322
 
323
+ auth()->user()->posts()->create($params);
324
+
325
- Post::create($params);
325
+ //Post::create($params);
326
326
 
327
327
 
328
328
 
@@ -354,6 +354,8 @@
354
354
 
355
355
  {
356
356
 
357
+
358
+
357
359
  $post=Post::findOrFail($post_id);
358
360
 
359
361
  $this->authorize('edit', $post);
@@ -382,10 +384,10 @@
382
384
 
383
385
  $post=Post::findOrFail($post_id);
384
386
 
387
+ $this->authorize('update', $post);
388
+
385
389
  $post->fill($params)->save();
386
390
 
387
- $this->authorize('update', $post);
388
-
389
391
  return redirect()->route('posts.show',['post'=>$post]);
390
392
 
391
393
  }
@@ -420,6 +422,8 @@
420
422
 
421
423
  }
422
424
 
425
+
426
+
423
427
  ```
424
428
 
425
429
  database/migrations/posts_table.php

3

コード修正しました。

2019/12/26 13:52

投稿

GO999
GO999

スコア5

test CHANGED
File without changes
test CHANGED
@@ -116,7 +116,7 @@
116
116
 
117
117
  namespace App\Policies;
118
118
 
119
-
119
+ use App\Post;
120
120
 
121
121
  use App\User;
122
122
 
@@ -128,7 +128,7 @@
128
128
 
129
129
  {
130
130
 
131
- // use HandlesAuthorization;
131
+ use HandlesAuthorization;
132
132
 
133
133
 
134
134
 
@@ -142,41 +142,45 @@
142
142
 
143
143
  */
144
144
 
145
- //public function __construct()
145
+ public function __construct()
146
-
146
+
147
- //{
147
+ {
148
148
 
149
149
  //
150
150
 
151
- //}
151
+ }
152
-
153
-
154
-
155
-
156
152
 
157
153
 
158
154
 
159
155
  public function edit(User $user, Post $post)
160
156
 
161
- {
157
+ {
162
-
158
+
163
- return $user->id === $post->user_id;
159
+ return $user->id === $post->user_id;
164
-
160
+
165
- }
161
+ }
166
-
167
-
168
-
162
+
163
+
164
+
169
- public function update(User $user, Post $post)
165
+ public function update(User $user, Post $post)
170
-
166
+
171
- {
167
+ {
172
-
168
+
173
- return $user->id === $post->user_id;
169
+ return $user->id === $post->user_id;
174
-
170
+
175
- }
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ }
176
180
 
177
181
  ```
178
182
 
179
-
183
+ App/Http/Policies/UserPolicy.php
180
184
 
181
185
  ```PHP
182
186
 
@@ -184,6 +188,76 @@
184
188
 
185
189
 
186
190
 
191
+ namespace App\Policies;
192
+
193
+
194
+
195
+ use App\Post;
196
+
197
+ use App\User;
198
+
199
+ use Illuminate\Auth\Access\HandlesAuthorization;
200
+
201
+
202
+
203
+ class UserPolicy
204
+
205
+ {
206
+
207
+ use HandlesAuthorization;
208
+
209
+
210
+
211
+ /**
212
+
213
+ * Create a new policy instance.
214
+
215
+ *
216
+
217
+ * @return void
218
+
219
+ */
220
+
221
+ public function __construct()
222
+
223
+ {
224
+
225
+ //
226
+
227
+ }
228
+
229
+
230
+
231
+ public function edit(User $user, User $model)
232
+
233
+ {
234
+
235
+ return $user->id == $model->id;
236
+
237
+ }
238
+
239
+
240
+
241
+ public function update(User $user, User $model)
242
+
243
+ {
244
+
245
+ return $user->id == $model->id;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ ```
252
+
253
+ App/Http/Controllers/PostsController.php
254
+
255
+ ```PHP
256
+
257
+ <?php
258
+
259
+
260
+
187
261
  namespace App\Http\Controllers;
188
262
 
189
263
 
@@ -310,7 +384,7 @@
310
384
 
311
385
  $post->fill($params)->save();
312
386
 
313
- $this->authorize('edit', $post);
387
+ $this->authorize('update', $post);
314
388
 
315
389
  return redirect()->route('posts.show',['post'=>$post]);
316
390
 
@@ -348,7 +422,7 @@
348
422
 
349
423
  ```
350
424
 
351
- App/Http/Controllers/PostsController.php
425
+ database/migrations/posts_table.php
352
426
 
353
427
  ```PHP
354
428
 
@@ -356,171 +430,77 @@
356
430
 
357
431
 
358
432
 
359
- namespace App\Http\Controllers;
360
-
361
-
362
-
363
- use Illuminate\Http\Request;
433
+ use Illuminate\Support\Facades\Schema;
364
-
434
+
365
- use App\Post;
435
+ use Illuminate\Database\Schema\Blueprint;
436
+
366
-
437
+ use Illuminate\Database\Migrations\Migration;
367
-
368
-
438
+
439
+
440
+
369
- class PostsController extends Controller
441
+ class CreatePostsTable extends Migration
370
442
 
371
443
  {
372
444
 
373
- public function index()
374
-
375
- {
376
-
377
- return view('posts.top');
378
-
379
-
380
-
381
- }
382
-
383
-
384
-
385
- public function bbs()
386
-
387
- {
388
-
389
- $posts = Post::with(['comments'])->orderBy('created_at', 'desc')->paginate(10);
390
-
391
-
392
-
393
- return view('posts.bbs',['posts'=>$posts]);
394
-
395
- }
396
-
397
-
398
-
399
- public function create()
400
-
401
- {
402
-
403
- return view('posts.create');
404
-
405
- }
406
-
407
-
408
-
409
- public function store(Request $request)
410
-
411
- {
412
-
413
- $params = $request->validate([
414
-
415
- 'title' => 'required|max:50',
416
-
417
- 'body' => 'required|max:2000',
418
-
419
- ]);
420
-
421
-
422
-
423
- Post::create($params);
424
-
425
-
426
-
427
- return redirect()->route('bbsTop');
428
-
429
- }
430
-
431
-
432
-
433
- public function show($post_id)
434
-
435
- {
436
-
437
- $post=Post::findOrFail($post_id);
438
-
439
-
440
-
441
- return view('posts.show',[
442
-
443
- 'post'=>$post,
444
-
445
- ]);
446
-
447
- }
448
-
449
-
450
-
451
- public function edit($post_id)
452
-
453
- {
454
-
455
- $post=Post::findOrFail($post_id);
456
-
457
- $this->authorize('edit', $post);
458
-
459
- return view('posts.edit', [
460
-
461
- 'post' => $post,
462
-
463
- ]);
464
-
465
- }
466
-
467
-
468
-
469
- public function update($post_id,Request $request)
470
-
471
- {
472
-
473
- $params=$request->validate([
474
-
475
- 'title'=>'required|max:50',
476
-
477
- 'body'=>'required|max:2000',
478
-
479
- ]);
480
-
481
- $post=Post::findOrFail($post_id);
482
-
483
- $post->fill($params)->save();
484
-
485
- $this->authorize('update', $post);
486
-
487
- return redirect()->route('posts.show',['post'=>$post]);
488
-
489
- }
490
-
491
-
492
-
493
- public function destroy($post_id)
494
-
495
- {
496
-
497
- $post=Post::findOrFail($post_id);
498
-
499
-
500
-
501
- \DB::transaction(function() use ($post){
502
-
503
- $post->comments()->delete();
504
-
505
- $post->delete();
506
-
507
- });
508
-
509
-
510
-
511
- return redirect()->route('bbsTop');
512
-
513
- }
514
-
515
-
516
-
517
-
518
-
519
- }
445
+ /**
446
+
447
+ * Run the migrations.
448
+
449
+ *
450
+
451
+ * @return void
452
+
453
+ */
454
+
455
+ public function up()
456
+
457
+ {
458
+
459
+ Schema::create('posts', function (Blueprint $table) {
460
+
461
+ $table->increments('id');
462
+
463
+ $table->unsignedInteger('user_id');
464
+
465
+ $table->string('title',50);
466
+
467
+ $table->text('body');
468
+
469
+ $table->timestamps();
470
+
471
+
472
+
473
+ $table->foreign('user_id')->references('id')->on('users');
474
+
475
+ });
476
+
477
+ }
478
+
479
+
480
+
481
+ /**
482
+
483
+ * Reverse the migrations.
484
+
485
+ *
486
+
487
+ * @return void
488
+
489
+ */
490
+
491
+ public function down()
492
+
493
+ {
494
+
495
+ Schema::dropIfExists('posts');
496
+
497
+ }
498
+
499
+ }
520
500
 
521
501
  ```
522
502
 
523
- database/migrations/posts_table.php
503
+ database/migrations/users_table.php
524
504
 
525
505
  ```PHP
526
506
 
@@ -536,7 +516,7 @@
536
516
 
537
517
 
538
518
 
539
- class CreatePostsTable extends Migration
519
+ class CreateUsersTable extends Migration
540
520
 
541
521
  {
542
522
 
@@ -554,22 +534,22 @@
554
534
 
555
535
  {
556
536
 
557
- Schema::create('posts', function (Blueprint $table) {
537
+ Schema::create('users', function (Blueprint $table) {
558
-
538
+
559
- $table->increments('id');
539
+ $table->bigIncrements('id');
560
-
561
- $table->unsignedInteger('user_id');
540
+
562
-
563
- $table->string('title',50);
541
+ $table->string('name');
542
+
564
-
543
+ $table->string('email')->unique();
544
+
545
+ $table->timestamp('email_verified_at')->nullable();
546
+
565
- $table->text('body');
547
+ $table->string('password');
548
+
549
+ $table->rememberToken();
566
550
 
567
551
  $table->timestamps();
568
552
 
569
-
570
-
571
- $table->foreign('user_id')->references('id')->on('users');
572
-
573
553
  });
574
554
 
575
555
  }
@@ -590,7 +570,7 @@
590
570
 
591
571
  {
592
572
 
593
- Schema::dropIfExists('posts');
573
+ Schema::dropIfExists('users');
594
574
 
595
575
  }
596
576
 
@@ -598,7 +578,7 @@
598
578
 
599
579
  ```
600
580
 
601
- database/migrations/users_table.php
581
+ App/Http/Providers/Post.php
602
582
 
603
583
  ```PHP
604
584
 
@@ -606,77 +586,55 @@
606
586
 
607
587
 
608
588
 
609
- use Illuminate\Support\Facades\Schema;
589
+ namespace App;
610
-
590
+
591
+
592
+
611
- use Illuminate\Database\Schema\Blueprint;
593
+ use Illuminate\Database\Eloquent\Model;
612
-
613
- use Illuminate\Database\Migrations\Migration;
594
+
614
-
615
-
616
-
595
+
596
+
617
- class CreateUsersTable extends Migration
597
+ class Post extends Model
618
598
 
619
599
  {
620
600
 
621
- /**
622
-
623
- * Run the migrations.
601
+ protected $fillable = [
624
-
625
- *
602
+
626
-
627
- * @return void
603
+ 'title',
628
-
604
+
629
- */
605
+ 'body',
606
+
630
-
607
+ ];
608
+
609
+
610
+
631
- public function up()
611
+ public function comments()
632
-
612
+
633
- {
613
+ {
634
-
635
- Schema::create('users', function (Blueprint $table) {
614
+
636
-
637
- $table->bigIncrements('id');
638
-
639
- $table->string('name');
640
-
641
- $table->string('email')->unique();
642
-
643
- $table->timestamp('email_verified_at')->nullable();
644
-
645
- $table->string('password');
615
+ return $this->hasMany('App\Comment');
646
-
647
- $table->rememberToken();
616
+
648
-
649
- $table->timestamps();
650
-
651
- });
652
-
653
- }
617
+ }
654
-
655
-
656
-
657
- /**
618
+
658
-
659
- * Reverse the migrations.
619
+
660
-
661
- *
620
+
662
-
663
- * @return void
664
-
665
- */
666
-
667
- public function down()
621
+ public function user()
668
-
622
+
669
- {
623
+ {
670
-
624
+
671
- Schema::dropIfExists('users');
625
+ return $this->belongsTo('App\User');
672
-
626
+
673
- }
627
+ }
674
628
 
675
629
  }
676
630
 
631
+
632
+
677
633
  ```
678
634
 
635
+
636
+
679
- App/Http/Providers/Post.php
637
+ App/Http/Providers/User.php
680
638
 
681
639
  ```PHP
682
640
 
@@ -688,150 +646,94 @@
688
646
 
689
647
 
690
648
 
691
- use Illuminate\Database\Eloquent\Model;
649
+ use Illuminate\Notifications\Notifiable;
650
+
692
-
651
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
652
+
693
-
653
+ use Illuminate\Foundation\Auth\User as Authenticatable;
694
-
654
+
655
+
656
+
695
- class Post extends Model
657
+ class User extends Authenticatable
696
658
 
697
659
  {
698
660
 
661
+ use Notifiable;
662
+
663
+
664
+
665
+ /**
666
+
667
+ * The attributes that are mass assignable.
668
+
669
+ *
670
+
671
+ * @var array
672
+
673
+ */
674
+
699
675
  protected $fillable = [
700
676
 
701
- 'title',
677
+ 'name', 'email', 'password',
702
-
703
- 'body',
704
678
 
705
679
  ];
706
680
 
707
681
 
708
682
 
683
+ /**
684
+
685
+ * The attributes that should be hidden for arrays.
686
+
687
+ *
688
+
689
+ * @var array
690
+
691
+ */
692
+
709
- public function comments()
693
+ protected $hidden = [
710
-
711
- {
694
+
712
-
713
- return $this->hasMany('App\Comment');
695
+ 'password', 'remember_token',
696
+
714
-
697
+ ];
698
+
699
+
700
+
701
+ /**
702
+
703
+ * The attributes that should be cast to native types.
704
+
715
- }
705
+ *
706
+
716
-
707
+ * @var array
708
+
717
-
709
+ */
710
+
718
-
711
+ protected $casts = [
712
+
713
+ 'email_verified_at' => 'datetime',
714
+
715
+ ];
716
+
717
+
718
+
719
- public function user()
719
+ public function posts()
720
720
 
721
721
  {
722
722
 
723
- return $this->belongsTo('App\User');
723
+ return $this->hasMany('App\Post');
724
724
 
725
725
  }
726
726
 
727
+
728
+
729
+
730
+
727
731
  }
728
732
 
729
733
 
730
734
 
731
735
  ```
732
736
 
733
-
734
-
735
- App/Http/Providers/User.php
736
-
737
- ```PHP
738
-
739
- <?php
740
-
741
-
742
-
743
- namespace App;
744
-
745
-
746
-
747
- use Illuminate\Notifications\Notifiable;
748
-
749
- use Illuminate\Contracts\Auth\MustVerifyEmail;
750
-
751
- use Illuminate\Foundation\Auth\User as Authenticatable;
752
-
753
-
754
-
755
- class User extends Authenticatable
756
-
757
- {
758
-
759
- use Notifiable;
760
-
761
-
762
-
763
- /**
764
-
765
- * The attributes that are mass assignable.
766
-
767
- *
768
-
769
- * @var array
770
-
771
- */
772
-
773
- protected $fillable = [
774
-
775
- 'name', 'email', 'password',
776
-
777
- ];
778
-
779
-
780
-
781
- /**
782
-
783
- * The attributes that should be hidden for arrays.
784
-
785
- *
786
-
787
- * @var array
788
-
789
- */
790
-
791
- protected $hidden = [
792
-
793
- 'password', 'remember_token',
794
-
795
- ];
796
-
797
-
798
-
799
- /**
800
-
801
- * The attributes that should be cast to native types.
802
-
803
- *
804
-
805
- * @var array
806
-
807
- */
808
-
809
- protected $casts = [
810
-
811
- 'email_verified_at' => 'datetime',
812
-
813
- ];
814
-
815
-
816
-
817
- public function posts()
818
-
819
- {
820
-
821
- return $this->hasMany('App\Post');
822
-
823
- }
824
-
825
-
826
-
827
-
828
-
829
- }
830
-
831
-
832
-
833
- ```
834
-
835
737
  ### 試したこと
836
738
 
837
739
 

2

コードを修正しました。

2019/12/22 09:55

投稿

GO999
GO999

スコア5

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,13 @@
36
36
 
37
37
 
38
38
 
39
-
39
+ use App\Post;
40
+
41
+ use App\Policies\PostPolicy;
42
+
43
+ use App\User;
44
+
45
+ use App\Policies\UserPolicy;
40
46
 
41
47
  use Illuminate\Support\Facades\Gate;
42
48
 
@@ -96,6 +102,8 @@
96
102
 
97
103
  }
98
104
 
105
+
106
+
99
107
  ```
100
108
 
101
109
  App/Http/Policies/PostPolicy.php
@@ -474,7 +482,7 @@
474
482
 
475
483
  $post->fill($params)->save();
476
484
 
477
- $this->authorize('edit', $post);
485
+ $this->authorize('update', $post);
478
486
 
479
487
  return redirect()->route('posts.show',['post'=>$post]);
480
488
 

1

参考サイトを追加しました。

2019/12/22 03:26

投稿

GO999
GO999

スコア5

test CHANGED
File without changes
test CHANGED
@@ -830,7 +830,11 @@
830
830
 
831
831
  参照サイトを参考にポリシーの作成・登録、ルールの記述、コントローラに認可を追加しました。何度やっても上手くいかなかったので、Post.php,User.phpと、テーブルも確認し記述し直しましたが、投稿者IDとユーザーIDを合致させて認可することができませんでした。
832
832
 
833
-
833
+ 参照サイト:https://readouble.com/laravel/5.7/ja/authorization.html#writing-policies
834
+
835
+      https://qiita.com/sutara79/items/11d8417a6fc91ca1b841
836
+
837
+      https://tech.windii.jp/backend/laravel/authorization-basic
834
838
 
835
839
  ### 補足情報(FW/ツールのバージョンなど)
836
840