質問編集履歴

3

Laravelを追加しました。

2020/07/19 09:58

投稿

kazu100817
kazu100817

スコア2

test CHANGED
File without changes
test CHANGED
@@ -24,31 +24,775 @@
24
24
 
25
25
  ### 該当のソースコード
26
26
 
27
-
27
+ [リンク内容](https://i.gyazo.com/2cc080f178d0b70048b7f87968203508.png)
28
-
28
+
29
- favorite_buttonn.blade.php
29
+ User.php
30
30
 
31
31
  ```
32
32
 
33
- @if (Auth::id() != $microposts->id)
34
-
35
- @if (Auth::user()->is_favoriting($microposts->id))
36
-
37
- {{-- お気に入りを外すボタンのフォーム --}}
38
-
39
- {!! Form::open(['route' => ['favorites.unfavorite', $microposts->id], 'method' => 'delete']) !!}
40
-
41
- {!! Form::submit('Unfavorite', ['class' => "btn btn-danger btn-block"]) !!}
33
+ <?php
34
+
35
+
36
+
37
+ namespace App;
38
+
39
+
40
+
41
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
42
+
43
+ use Illuminate\Foundation\Auth\User as Authenticatable;
44
+
45
+ use Illuminate\Notifications\Notifiable;
46
+
47
+
48
+
49
+ class User extends Authenticatable
50
+
51
+ {
52
+
53
+ use Notifiable;
54
+
55
+
56
+
57
+ /**
58
+
59
+ * The attributes that are mass assignable.
60
+
61
+ *
62
+
63
+ * @var array
64
+
65
+ */
66
+
67
+ protected $fillable = [
68
+
69
+ 'name', 'email', 'password',
70
+
71
+ ];
72
+
73
+
74
+
75
+ /**
76
+
77
+ * The attributes that should be hidden for arrays.
78
+
79
+ *
80
+
81
+ * @var array
82
+
83
+ */
84
+
85
+ protected $hidden = [
86
+
87
+ 'password', 'remember_token',
88
+
89
+ ];
90
+
91
+
92
+
93
+ /**
94
+
95
+ * The attributes that should be cast to native types.
96
+
97
+ *
98
+
99
+ * @var array
100
+
101
+ */
102
+
103
+ protected $casts = [
104
+
105
+ 'email_verified_at' => 'datetime',
106
+
107
+ ];
108
+
109
+
110
+
111
+ public function microposts()
112
+
113
+ {
114
+
115
+ return $this->hasMany(Micropost::class);
116
+
117
+ }
118
+
119
+
120
+
121
+
122
+
123
+ /**
124
+
125
+ * このユーザがフォロー中のユーザ。( Userモデルとの関係を定義)
126
+
127
+ */
128
+
129
+ public function followings()
130
+
131
+ {
132
+
133
+ return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps();
134
+
135
+ }
136
+
137
+
138
+
139
+ /**
140
+
141
+ * このユーザをフォロー中のユーザ。( Userモデルとの関係を定義)
142
+
143
+ */
144
+
145
+ public function followers()
146
+
147
+ {
148
+
149
+ return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps();
150
+
151
+ }
152
+
153
+
154
+
155
+ /**
156
+
157
+ * $userIdで指定されたユーザをフォローする。
158
+
159
+ *
160
+
161
+ * @param int $userId
162
+
163
+ * @return bool
164
+
165
+ */
166
+
167
+ public function follow($userId)
168
+
169
+ {
170
+
171
+ // すでにフォローしているかの確認
172
+
173
+ $exist = $this->is_following($userId);
174
+
175
+ // 相手が自分自身かどうかの確認
176
+
177
+ $its_me = $this->id == $userId;
178
+
179
+
180
+
181
+ if ($exist || $its_me) {
182
+
183
+ // すでにフォローしていれば何もしない
184
+
185
+ return false;
186
+
187
+ } else {
188
+
189
+ // 未フォローであればフォローする
190
+
191
+ $this->followings()->attach($userId);
192
+
193
+ return true;
194
+
195
+ }
196
+
197
+ }
198
+
199
+
200
+
201
+ /**
202
+
203
+ * $userIdで指定されたユーザをアンフォローする。
204
+
205
+ *
206
+
207
+ * @param int $userId
208
+
209
+ * @return bool
210
+
211
+ */
212
+
213
+ public function unfollow($userId)
214
+
215
+ {
216
+
217
+ // すでにフォローしているかの確認
218
+
219
+ $exist = $this->is_following($userId);
220
+
221
+ // 相手が自分自身かどうかの確認
222
+
223
+ $its_me = $this->id == $userId;
224
+
225
+
226
+
227
+ if ($exist && !$its_me) {
228
+
229
+ // すでにフォローしていればフォローを外す
230
+
231
+ $this->followings()->detach($userId);
232
+
233
+ return true;
234
+
235
+ } else {
236
+
237
+ // 未フォローであれば何もしない
238
+
239
+ return false;
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+ /**
248
+
249
+ * 指定された $userIdのユーザをこのユーザがフォロー中であるか調べる。フォロー中ならtrueを返す。
250
+
251
+ *
252
+
253
+ * @param int $userId
254
+
255
+ * @return bool
256
+
257
+ */
258
+
259
+ public function is_following($userId)
260
+
261
+ {
262
+
263
+ // フォロー中ユーザの中に $userIdのものが存在するか
264
+
265
+ return $this->followings()->where('follow_id', $userId)->exists();
266
+
267
+ }
268
+
269
+ /**
270
+
271
+ * このユーザに関係するモデルの件数をロードする。
272
+
273
+ */
274
+
275
+ public function loadRelationshipCounts()
276
+
277
+ {
278
+
279
+ $this->loadCount(['microposts', 'followings', 'followers', 'favorites']);
280
+
281
+ }
282
+
283
+
284
+
285
+ /**
286
+
287
+ * このユーザとフォロー中ユーザの投稿に絞り込む。
288
+
289
+ */
290
+
291
+ public function feed_microposts()
292
+
293
+ {
294
+
295
+ // このユーザがフォロー中のユーザのidを取得して配列にする
296
+
297
+ $userIds = $this->followings()->pluck('users.id')->toArray();
298
+
299
+ // このユーザのidもその配列に追加
300
+
301
+ $userIds[] = $this->id;
302
+
303
+ // それらのユーザが所有する投稿に絞り込む
304
+
305
+ return Micropost::whereIn('user_id', $userIds);
306
+
307
+ }
308
+
309
+
310
+
311
+ public function favorites()
312
+
313
+ {
314
+
315
+ return $this->belongsToMany(Micropost::class, 'user_favorite', 'user_id', 'micropost_id')->withTimestamps();
316
+
317
+ }
318
+
319
+
320
+
321
+ public function favorite($usetId)
322
+
323
+ {
324
+
325
+
326
+
327
+ $exist = $this->is_favoriting($userId);
328
+
329
+
330
+
331
+ $its_me = $this->id == $userId;
332
+
333
+
334
+
335
+ if ($exist || $its_me) {
336
+
337
+
338
+
339
+ return false;
340
+
341
+ } else {
342
+
343
+
344
+
345
+ $this->favoriting()->attach($userId);
346
+
347
+ return true;
348
+
349
+ }
350
+
351
+ }
352
+
353
+
354
+
355
+ public function unfavorite($userId)
356
+
357
+ {
358
+
359
+
360
+
361
+ $exist = $this->is_favoriting($userId);
362
+
363
+
364
+
365
+ $its_me = $this->id == $userId;
366
+
367
+
368
+
369
+ if ($exist && !$its_me) {
370
+
371
+
372
+
373
+ $this->favoriting()->detach($userId);
374
+
375
+ return true;
376
+
377
+ } else {
378
+
379
+
380
+
381
+ return false;
382
+
383
+ }
384
+
385
+ }
386
+
387
+
388
+
389
+ public function is_favoriting($micropostId)
390
+
391
+ {
392
+
393
+
394
+
395
+ return $this->favoriting()->where('micropost_id', $micropostId)->exists();
396
+
397
+ }
398
+
399
+ }
400
+
401
+ ```
402
+
403
+ UsersController.php
404
+
405
+ ```
406
+
407
+ <?php
408
+
409
+
410
+
411
+ namespace App\Http\Controllers;
412
+
413
+
414
+
415
+ use Illuminate\Http\Request;
416
+
417
+
418
+
419
+ use App\User;
420
+
421
+
422
+
423
+ use Appp\Micropost;
424
+
425
+
426
+
427
+ class UsersController extends Controller
428
+
429
+ {
430
+
431
+ /**
432
+
433
+ * Display a listing of the resource.
434
+
435
+ *
436
+
437
+ * @return \Illuminate\Http\Response
438
+
439
+ */
440
+
441
+ public function index()
442
+
443
+ {
444
+
445
+ // ユーザ一覧をidの降順で取得
446
+
447
+ $users = User::orderBy('id', 'desc')->paginate(10);
448
+
449
+
450
+
451
+ // ユーザ一覧ビューでそれを表示
452
+
453
+ return view('users.index', [
454
+
455
+ 'users' => $users,
456
+
457
+ ]);
458
+
459
+
460
+
461
+ }
462
+
463
+
464
+
465
+ /**
466
+
467
+ * Show the form for creating a new resource.
468
+
469
+ *
470
+
471
+ * @return \Illuminate\Http\Response
472
+
473
+ */
474
+
475
+ public function create()
476
+
477
+ {
478
+
479
+ //
480
+
481
+ }
482
+
483
+
484
+
485
+ /**
486
+
487
+ * Store a newly created resource in storage.
488
+
489
+ *
490
+
491
+ * @param \Illuminate\Http\Request $request
492
+
493
+ * @return \Illuminate\Http\Response
494
+
495
+ */
496
+
497
+ public function store(Request $request)
498
+
499
+ {
500
+
501
+ //
502
+
503
+ }
504
+
505
+
506
+
507
+ /**
508
+
509
+ * Display the specified resource.
510
+
511
+ *
512
+
513
+ * @param int $id
514
+
515
+ * @return \Illuminate\Http\Response
516
+
517
+ */
518
+
519
+ public function show($id)
520
+
521
+ {
522
+
523
+ // idの値でユーザを検索して取得
524
+
525
+ $user = User::findOrFail($id);
526
+
527
+
528
+
529
+ // 関係するモデルの件数をロード
530
+
531
+ $user->loadRelationshipCounts();
532
+
533
+
534
+
535
+ // ユーザの投稿一覧を作成日時の降順で取得
536
+
537
+ $microposts = $user->microposts()->orderBy('created_at', 'desc')->paginate(10);
538
+
539
+
540
+
541
+ // ユーザ詳細ビューでそれらを表示
542
+
543
+ return view('users.show', [
544
+
545
+ 'user' => $user,
546
+
547
+ 'microposts' => $microposts,
548
+
549
+ ]);
550
+
551
+ }
552
+
553
+
554
+
555
+ /**
556
+
557
+ * Show the form for editing the specified resource.
558
+
559
+ *
560
+
561
+ * @param int $id
562
+
563
+ * @return \Illuminate\Http\Response
564
+
565
+ */
566
+
567
+ public function edit($id)
568
+
569
+ {
570
+
571
+ //
572
+
573
+ }
574
+
575
+
576
+
577
+ /**
578
+
579
+ * Update the specified resource in storage.
580
+
581
+ *
582
+
583
+ * @param \Illuminate\Http\Request $request
584
+
585
+ * @param int $id
586
+
587
+ * @return \Illuminate\Http\Response
588
+
589
+ */
590
+
591
+ public function update(Request $request, $id)
592
+
593
+ {
594
+
595
+ //
596
+
597
+ }
598
+
599
+
600
+
601
+ /**
602
+
603
+ * Remove the specified resource from storage.
604
+
605
+ *
606
+
607
+ * @param int $id
608
+
609
+ * @return \Illuminate\Http\Response
610
+
611
+ */
612
+
613
+ public function destroy($id)
614
+
615
+ {
616
+
617
+ $micropost = \App\Micropost::findOrFail($id);
618
+
619
+
620
+
621
+ // 認証済みユーザ(閲覧者)がその投稿の所有者である場合は、投稿を削除
622
+
623
+ if (\Auth::id() === $micropost->user_id) {
624
+
625
+ $micropost->delete();
626
+
627
+ }
628
+
629
+
630
+
631
+ // 前のURLへリダイレクトさせる
632
+
633
+ return back();
634
+
635
+ }
636
+
637
+
638
+
639
+ /**
640
+
641
+ * ユーザのフォロー一覧ページを表示するアクション。
642
+
643
+ *
644
+
645
+ * @param $id ユーザのid
646
+
647
+ * @return \Illuminate\Http\Response
648
+
649
+ */
650
+
651
+ public function followings($id)
652
+
653
+ {
654
+
655
+ // idの値でユーザを検索して取得
656
+
657
+ $user = User::findOrFail($id);
658
+
659
+
660
+
661
+ // 関係するモデルの件数をロード
662
+
663
+ $user->loadRelationshipCounts();
664
+
665
+
666
+
667
+ // ユーザのフォロー一覧を取得
668
+
669
+ $followings = $user->followings()->paginate(10);
670
+
671
+
672
+
673
+ // フォロー一覧ビューでそれらを表示
674
+
675
+ return view('users.followings', [
676
+
677
+ 'user' => $user,
678
+
679
+ 'users' => $followings,
680
+
681
+ ]);
682
+
683
+ }
684
+
685
+
686
+
687
+ /**
688
+
689
+ * ユーザのフォロワー一覧ページを表示するアクション。
690
+
691
+ *
692
+
693
+ * @param $id ユーザのid
694
+
695
+ * @return \Illuminate\Http\Response
696
+
697
+ */
698
+
699
+ public function followers($id)
700
+
701
+ {
702
+
703
+ // idの値でユーザを検索して取得
704
+
705
+ $user = User::findOrFail($id);
706
+
707
+
708
+
709
+ // 関係するモデルの件数をロード
710
+
711
+ $user->loadRelationshipCounts();
712
+
713
+
714
+
715
+ // ユーザのフォロワー一覧を取得
716
+
717
+ $followers = $user->followers()->paginate(10);
718
+
719
+
720
+
721
+ // フォロワー一覧ビューでそれらを表示
722
+
723
+ return view('users.followers', [
724
+
725
+ 'user' => $user,
726
+
727
+ 'users' => $followers,
728
+
729
+ ]);
730
+
731
+ }
732
+
733
+
734
+
735
+ public function favorites($id)
736
+
737
+ {
738
+
739
+ $user = User::findOrFail($id);
740
+
741
+
742
+
743
+ $user->loadRelationshipCounts();
744
+
745
+
746
+
747
+ $favorites = $user->favorites()->paginate(10);
748
+
749
+
750
+
751
+ return view('users.favorites', [
752
+
753
+ 'user' => $user,
754
+
755
+ 'users' => $favorites,
756
+
757
+ ]);
758
+
759
+ }
760
+
761
+ }
762
+
763
+ ```
764
+
765
+
766
+
767
+ ### 補足情報(FW/ツールのバージョンなど)
768
+
769
+ 使っているクラウドはCloud9です。
770
+
771
+ ここにより詳細な情報を記載してください。
772
+
773
+ エラーが出た文は本に書いてあったのを見て書きました下記に記載します。
774
+
775
+ ```
776
+
777
+ @if (Auth::id() != $user->id)
778
+
779
+ @if (Auth::user()->is_following($user->id))
780
+
781
+ {{-- アンフォローボタンのフォーム --}}
782
+
783
+ {!! Form::open(['route' => ['user.unfollow', $user->id], 'method' => 'delete']) !!}
784
+
785
+ {!! Form::submit('Unfollow', ['class' => "btn btn-danger btn-block"]) !!}
42
786
 
43
787
  {!! Form::close() !!}
44
788
 
45
789
  @else
46
790
 
47
- {{-- お気に入りボタンのフォーム --}}
791
+ {{-- フォローボタンのフォーム --}}
48
-
792
+
49
- {!! Form::open(['route' => ['favorites.favorite', $microposts->id]]) !!}
793
+ {!! Form::open(['route' => ['user.follow', $user->id]]) !!}
50
-
794
+
51
- {!! Form::submit('Favorite', ['class' => "btn btn-primary btn-block"]) !!}
795
+ {!! Form::submit('Follow', ['class' => "btn btn-primary btn-block"]) !!}
52
796
 
53
797
  {!! Form::close() !!}
54
798
 
@@ -57,427 +801,3 @@
57
801
  @endif
58
802
 
59
803
  ```
60
-
61
- User.php
62
-
63
- ```
64
-
65
- <?php
66
-
67
-
68
-
69
- namespace App;
70
-
71
-
72
-
73
- use Illuminate\Contracts\Auth\MustVerifyEmail;
74
-
75
- use Illuminate\Foundation\Auth\User as Authenticatable;
76
-
77
- use Illuminate\Notifications\Notifiable;
78
-
79
-
80
-
81
- class User extends Authenticatable
82
-
83
- {
84
-
85
- use Notifiable;
86
-
87
-
88
-
89
- /**
90
-
91
- * The attributes that are mass assignable.
92
-
93
- *
94
-
95
- * @var array
96
-
97
- */
98
-
99
- protected $fillable = [
100
-
101
- 'name', 'email', 'password',
102
-
103
- ];
104
-
105
-
106
-
107
- /**
108
-
109
- * The attributes that should be hidden for arrays.
110
-
111
- *
112
-
113
- * @var array
114
-
115
- */
116
-
117
- protected $hidden = [
118
-
119
- 'password', 'remember_token',
120
-
121
- ];
122
-
123
-
124
-
125
- /**
126
-
127
- * The attributes that should be cast to native types.
128
-
129
- *
130
-
131
- * @var array
132
-
133
- */
134
-
135
- protected $casts = [
136
-
137
- 'email_verified_at' => 'datetime',
138
-
139
- ];
140
-
141
-
142
-
143
- public function microposts()
144
-
145
- {
146
-
147
- return $this->hasMany(Micropost::class);
148
-
149
- }
150
-
151
-
152
-
153
-
154
-
155
- /**
156
-
157
- * このユーザがフォロー中のユーザ。( Userモデルとの関係を定義)
158
-
159
- */
160
-
161
- public function followings()
162
-
163
- {
164
-
165
- return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps();
166
-
167
- }
168
-
169
-
170
-
171
- /**
172
-
173
- * このユーザをフォロー中のユーザ。( Userモデルとの関係を定義)
174
-
175
- */
176
-
177
- public function followers()
178
-
179
- {
180
-
181
- return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps();
182
-
183
- }
184
-
185
-
186
-
187
- /**
188
-
189
- * $userIdで指定されたユーザをフォローする。
190
-
191
- *
192
-
193
- * @param int $userId
194
-
195
- * @return bool
196
-
197
- */
198
-
199
- public function follow($userId)
200
-
201
- {
202
-
203
- // すでにフォローしているかの確認
204
-
205
- $exist = $this->is_following($userId);
206
-
207
- // 相手が自分自身かどうかの確認
208
-
209
- $its_me = $this->id == $userId;
210
-
211
-
212
-
213
- if ($exist || $its_me) {
214
-
215
- // すでにフォローしていれば何もしない
216
-
217
- return false;
218
-
219
- } else {
220
-
221
- // 未フォローであればフォローする
222
-
223
- $this->followings()->attach($userId);
224
-
225
- return true;
226
-
227
- }
228
-
229
- }
230
-
231
-
232
-
233
- /**
234
-
235
- * $userIdで指定されたユーザをアンフォローする。
236
-
237
- *
238
-
239
- * @param int $userId
240
-
241
- * @return bool
242
-
243
- */
244
-
245
- public function unfollow($userId)
246
-
247
- {
248
-
249
- // すでにフォローしているかの確認
250
-
251
- $exist = $this->is_following($userId);
252
-
253
- // 相手が自分自身かどうかの確認
254
-
255
- $its_me = $this->id == $userId;
256
-
257
-
258
-
259
- if ($exist && !$its_me) {
260
-
261
- // すでにフォローしていればフォローを外す
262
-
263
- $this->followings()->detach($userId);
264
-
265
- return true;
266
-
267
- } else {
268
-
269
- // 未フォローであれば何もしない
270
-
271
- return false;
272
-
273
- }
274
-
275
- }
276
-
277
-
278
-
279
- /**
280
-
281
- * 指定された $userIdのユーザをこのユーザがフォロー中であるか調べる。フォロー中ならtrueを返す。
282
-
283
- *
284
-
285
- * @param int $userId
286
-
287
- * @return bool
288
-
289
- */
290
-
291
- public function is_following($userId)
292
-
293
- {
294
-
295
- // フォロー中ユーザの中に $userIdのものが存在するか
296
-
297
- return $this->followings()->where('follow_id', $userId)->exists();
298
-
299
- }
300
-
301
- /**
302
-
303
- * このユーザに関係するモデルの件数をロードする。
304
-
305
- */
306
-
307
- public function loadRelationshipCounts()
308
-
309
- {
310
-
311
- $this->loadCount(['microposts', 'followings', 'followers', 'favorites']);
312
-
313
- }
314
-
315
-
316
-
317
- /**
318
-
319
- * このユーザとフォロー中ユーザの投稿に絞り込む。
320
-
321
- */
322
-
323
- public function feed_microposts()
324
-
325
- {
326
-
327
- // このユーザがフォロー中のユーザのidを取得して配列にする
328
-
329
- $userIds = $this->followings()->pluck('users.id')->toArray();
330
-
331
- // このユーザのidもその配列に追加
332
-
333
- $userIds[] = $this->id;
334
-
335
- // それらのユーザが所有する投稿に絞り込む
336
-
337
- return Micropost::whereIn('user_id', $userIds);
338
-
339
- }
340
-
341
-
342
-
343
- public function favorites()
344
-
345
- {
346
-
347
- return $this->belongsToMany(Micropost::class, 'user_favorite', 'user_id', 'micropost_id')->withTimestamps();
348
-
349
- }
350
-
351
-
352
-
353
- public function favorite($usetId)
354
-
355
- {
356
-
357
-
358
-
359
- $exist = $this->is_favoriting($userId);
360
-
361
-
362
-
363
- $its_me = $this->id == $userId;
364
-
365
-
366
-
367
- if ($exist || $its_me) {
368
-
369
-
370
-
371
- return false;
372
-
373
- } else {
374
-
375
-
376
-
377
- $this->favoriting()->attach($userId);
378
-
379
- return true;
380
-
381
- }
382
-
383
- }
384
-
385
-
386
-
387
- public function unfavorite($userId)
388
-
389
- {
390
-
391
-
392
-
393
- $exist = $this->is_favoriting($userId);
394
-
395
-
396
-
397
- $its_me = $this->id == $userId;
398
-
399
-
400
-
401
- if ($exist && !$its_me) {
402
-
403
-
404
-
405
- $this->favoriting()->detach($userId);
406
-
407
- return true;
408
-
409
- } else {
410
-
411
-
412
-
413
- return false;
414
-
415
- }
416
-
417
- }
418
-
419
-
420
-
421
- public function is_favoriting($micropostId)
422
-
423
- {
424
-
425
-
426
-
427
- return $this->favoriting()->where('micropost_id', $micropostId)->exists();
428
-
429
- }
430
-
431
- }
432
-
433
- ```
434
-
435
-
436
-
437
-
438
-
439
- ### 試したこと
440
-
441
-
442
-
443
- ここに問題に対して試したことを記載してください。
444
-
445
-
446
-
447
- ### 補足情報(FW/ツールのバージョンなど)
448
-
449
- 使っているクラウドはCloud9です。
450
-
451
- ここにより詳細な情報を記載してください。
452
-
453
- エラーが出た文は本に書いてあったのを見て書きました下記に記載します。
454
-
455
- ```
456
-
457
- @if (Auth::id() != $user->id)
458
-
459
- @if (Auth::user()->is_following($user->id))
460
-
461
- {{-- アンフォローボタンのフォーム --}}
462
-
463
- {!! Form::open(['route' => ['user.unfollow', $user->id], 'method' => 'delete']) !!}
464
-
465
- {!! Form::submit('Unfollow', ['class' => "btn btn-danger btn-block"]) !!}
466
-
467
- {!! Form::close() !!}
468
-
469
- @else
470
-
471
- {{-- フォローボタンのフォーム --}}
472
-
473
- {!! Form::open(['route' => ['user.follow', $user->id]]) !!}
474
-
475
- {!! Form::submit('Follow', ['class' => "btn btn-primary btn-block"]) !!}
476
-
477
- {!! Form::close() !!}
478
-
479
- @endif
480
-
481
- @endif
482
-
483
- ```

2

脱字がありました。

2020/07/19 09:58

投稿

kazu100817
kazu100817

スコア2

test CHANGED
File without changes
test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  @if (Auth::id() != $microposts->id)
34
34
 
35
- @if (Auth::user()->is_favoriteing($microposts->id))
35
+ @if (Auth::user()->is_favoriting($microposts->id))
36
36
 
37
37
  {{-- お気に入りを外すボタンのフォーム --}}
38
38
 
@@ -356,7 +356,7 @@
356
356
 
357
357
 
358
358
 
359
- $exist = $this->is_favoriteing($userId);
359
+ $exist = $this->is_favoriting($userId);
360
360
 
361
361
 
362
362
 
@@ -374,7 +374,7 @@
374
374
 
375
375
 
376
376
 
377
- $this->favoriteing()->attach($userId);
377
+ $this->favoriting()->attach($userId);
378
378
 
379
379
  return true;
380
380
 
@@ -390,7 +390,7 @@
390
390
 
391
391
 
392
392
 
393
- $exist = $this->is_favoriteing($userId);
393
+ $exist = $this->is_favoriting($userId);
394
394
 
395
395
 
396
396
 
@@ -402,7 +402,7 @@
402
402
 
403
403
 
404
404
 
405
- $this->favoriteing()->detach($userId);
405
+ $this->favoriting()->detach($userId);
406
406
 
407
407
  return true;
408
408
 
@@ -418,13 +418,13 @@
418
418
 
419
419
 
420
420
 
421
- public function is_favoriteing($micropostId)
421
+ public function is_favoriting($micropostId)
422
-
422
+
423
- {
423
+ {
424
-
425
-
426
-
424
+
425
+
426
+
427
- return $this->favoriteing()->where('micropost_id', $micropostId)->exists();
427
+ return $this->favoriting()->where('micropost_id', $micropostId)->exists();
428
428
 
429
429
  }
430
430
 

1

必要な文が抜けていました。

2020/07/19 09:35

投稿

kazu100817
kazu100817

スコア2

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,9 @@
26
26
 
27
27
 
28
28
 
29
+ favorite_buttonn.blade.php
30
+
29
- ```ここに言語名を入力
31
+ ```
30
32
 
31
33
  @if (Auth::id() != $microposts->id)
32
34
 
@@ -56,6 +58,382 @@
56
58
 
57
59
  ```
58
60
 
61
+ User.php
62
+
63
+ ```
64
+
65
+ <?php
66
+
67
+
68
+
69
+ namespace App;
70
+
71
+
72
+
73
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
74
+
75
+ use Illuminate\Foundation\Auth\User as Authenticatable;
76
+
77
+ use Illuminate\Notifications\Notifiable;
78
+
79
+
80
+
81
+ class User extends Authenticatable
82
+
83
+ {
84
+
85
+ use Notifiable;
86
+
87
+
88
+
89
+ /**
90
+
91
+ * The attributes that are mass assignable.
92
+
93
+ *
94
+
95
+ * @var array
96
+
97
+ */
98
+
99
+ protected $fillable = [
100
+
101
+ 'name', 'email', 'password',
102
+
103
+ ];
104
+
105
+
106
+
107
+ /**
108
+
109
+ * The attributes that should be hidden for arrays.
110
+
111
+ *
112
+
113
+ * @var array
114
+
115
+ */
116
+
117
+ protected $hidden = [
118
+
119
+ 'password', 'remember_token',
120
+
121
+ ];
122
+
123
+
124
+
125
+ /**
126
+
127
+ * The attributes that should be cast to native types.
128
+
129
+ *
130
+
131
+ * @var array
132
+
133
+ */
134
+
135
+ protected $casts = [
136
+
137
+ 'email_verified_at' => 'datetime',
138
+
139
+ ];
140
+
141
+
142
+
143
+ public function microposts()
144
+
145
+ {
146
+
147
+ return $this->hasMany(Micropost::class);
148
+
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+ /**
156
+
157
+ * このユーザがフォロー中のユーザ。( Userモデルとの関係を定義)
158
+
159
+ */
160
+
161
+ public function followings()
162
+
163
+ {
164
+
165
+ return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps();
166
+
167
+ }
168
+
169
+
170
+
171
+ /**
172
+
173
+ * このユーザをフォロー中のユーザ。( Userモデルとの関係を定義)
174
+
175
+ */
176
+
177
+ public function followers()
178
+
179
+ {
180
+
181
+ return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps();
182
+
183
+ }
184
+
185
+
186
+
187
+ /**
188
+
189
+ * $userIdで指定されたユーザをフォローする。
190
+
191
+ *
192
+
193
+ * @param int $userId
194
+
195
+ * @return bool
196
+
197
+ */
198
+
199
+ public function follow($userId)
200
+
201
+ {
202
+
203
+ // すでにフォローしているかの確認
204
+
205
+ $exist = $this->is_following($userId);
206
+
207
+ // 相手が自分自身かどうかの確認
208
+
209
+ $its_me = $this->id == $userId;
210
+
211
+
212
+
213
+ if ($exist || $its_me) {
214
+
215
+ // すでにフォローしていれば何もしない
216
+
217
+ return false;
218
+
219
+ } else {
220
+
221
+ // 未フォローであればフォローする
222
+
223
+ $this->followings()->attach($userId);
224
+
225
+ return true;
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+ /**
234
+
235
+ * $userIdで指定されたユーザをアンフォローする。
236
+
237
+ *
238
+
239
+ * @param int $userId
240
+
241
+ * @return bool
242
+
243
+ */
244
+
245
+ public function unfollow($userId)
246
+
247
+ {
248
+
249
+ // すでにフォローしているかの確認
250
+
251
+ $exist = $this->is_following($userId);
252
+
253
+ // 相手が自分自身かどうかの確認
254
+
255
+ $its_me = $this->id == $userId;
256
+
257
+
258
+
259
+ if ($exist && !$its_me) {
260
+
261
+ // すでにフォローしていればフォローを外す
262
+
263
+ $this->followings()->detach($userId);
264
+
265
+ return true;
266
+
267
+ } else {
268
+
269
+ // 未フォローであれば何もしない
270
+
271
+ return false;
272
+
273
+ }
274
+
275
+ }
276
+
277
+
278
+
279
+ /**
280
+
281
+ * 指定された $userIdのユーザをこのユーザがフォロー中であるか調べる。フォロー中ならtrueを返す。
282
+
283
+ *
284
+
285
+ * @param int $userId
286
+
287
+ * @return bool
288
+
289
+ */
290
+
291
+ public function is_following($userId)
292
+
293
+ {
294
+
295
+ // フォロー中ユーザの中に $userIdのものが存在するか
296
+
297
+ return $this->followings()->where('follow_id', $userId)->exists();
298
+
299
+ }
300
+
301
+ /**
302
+
303
+ * このユーザに関係するモデルの件数をロードする。
304
+
305
+ */
306
+
307
+ public function loadRelationshipCounts()
308
+
309
+ {
310
+
311
+ $this->loadCount(['microposts', 'followings', 'followers', 'favorites']);
312
+
313
+ }
314
+
315
+
316
+
317
+ /**
318
+
319
+ * このユーザとフォロー中ユーザの投稿に絞り込む。
320
+
321
+ */
322
+
323
+ public function feed_microposts()
324
+
325
+ {
326
+
327
+ // このユーザがフォロー中のユーザのidを取得して配列にする
328
+
329
+ $userIds = $this->followings()->pluck('users.id')->toArray();
330
+
331
+ // このユーザのidもその配列に追加
332
+
333
+ $userIds[] = $this->id;
334
+
335
+ // それらのユーザが所有する投稿に絞り込む
336
+
337
+ return Micropost::whereIn('user_id', $userIds);
338
+
339
+ }
340
+
341
+
342
+
343
+ public function favorites()
344
+
345
+ {
346
+
347
+ return $this->belongsToMany(Micropost::class, 'user_favorite', 'user_id', 'micropost_id')->withTimestamps();
348
+
349
+ }
350
+
351
+
352
+
353
+ public function favorite($usetId)
354
+
355
+ {
356
+
357
+
358
+
359
+ $exist = $this->is_favoriteing($userId);
360
+
361
+
362
+
363
+ $its_me = $this->id == $userId;
364
+
365
+
366
+
367
+ if ($exist || $its_me) {
368
+
369
+
370
+
371
+ return false;
372
+
373
+ } else {
374
+
375
+
376
+
377
+ $this->favoriteing()->attach($userId);
378
+
379
+ return true;
380
+
381
+ }
382
+
383
+ }
384
+
385
+
386
+
387
+ public function unfavorite($userId)
388
+
389
+ {
390
+
391
+
392
+
393
+ $exist = $this->is_favoriteing($userId);
394
+
395
+
396
+
397
+ $its_me = $this->id == $userId;
398
+
399
+
400
+
401
+ if ($exist && !$its_me) {
402
+
403
+
404
+
405
+ $this->favoriteing()->detach($userId);
406
+
407
+ return true;
408
+
409
+ } else {
410
+
411
+
412
+
413
+ return false;
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ public function is_favoriteing($micropostId)
422
+
423
+ {
424
+
425
+
426
+
427
+ return $this->favoriteing()->where('micropost_id', $micropostId)->exists();
428
+
429
+ }
430
+
431
+ }
432
+
433
+ ```
434
+
435
+
436
+
59
437
 
60
438
 
61
439
  ### 試したこと