質問編集履歴

3

ビュー部分のコードの修正

2022/10/21 11:42

投稿

takigawa777
takigawa777

スコア21

test CHANGED
File without changes
test CHANGED
@@ -197,7 +197,7 @@
197
197
  <li>書き込みはありません。</li>
198
198
  @endforelse
199
199
  </ul>
200
- {{ $posts->links() }}
200
+ {{ $like_posts->links() }}
201
201
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
202
202
  <script>
203
203
  /* global $ */

2

いいね一覧部分のindexコードの修正

2022/10/21 11:38

投稿

takigawa777
takigawa777

スコア21

test CHANGED
File without changes
test CHANGED
@@ -114,7 +114,7 @@
114
114
  // いいね一覧
115
115
  public function index()
116
116
  {
117
- $like_posts = \Auth::user()->likePosts->latest()->paginate(5);
117
+ $like_posts = \Auth::user()->likePosts()->latest()->paginate(5);
118
118
  return view('likes.index',[
119
119
  'title' => 'いいね一覧',
120
120
  'like_posts' => $like_posts,

1

ユーザーモデルの追加

2022/10/21 10:53

投稿

takigawa777
takigawa777

スコア21

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,88 @@
15
15
  ### 該当のソースコード
16
16
 
17
17
  ```ここに言語名を入力
18
+ User.php(ユーザーモデル)
19
+ <?php
20
+
21
+ namespace App;
22
+
23
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
24
+ use Illuminate\Foundation\Auth\User as Authenticatable;
25
+ use Illuminate\Notifications\Notifiable;
26
+
27
+ class User extends Authenticatable
28
+ {
29
+ use Notifiable;
30
+
31
+ /**
32
+ * The attributes that are mass assignable.
33
+ *
34
+ * @var array
35
+ */
36
+ protected $fillable = [
37
+ 'name', 'email', 'password', 'profile', 'image'
38
+ ];
39
+
40
+ /**
41
+ * The attributes that should be hidden for arrays.
42
+ *
43
+ * @var array
44
+ */
45
+ protected $hidden = [
46
+ 'password', 'remember_token',
47
+ ];
48
+
49
+ /**
50
+ * The attributes that should be cast to native types.
51
+ *
52
+ * @var array
53
+ */
54
+ protected $casts = [
55
+ 'email_verified_at' => 'datetime',
56
+ ];
57
+
58
+ // リレーションを設定
59
+ public function posts(){
60
+ return $this->hasMany('App\Post');
61
+ }
62
+
63
+ public function scopeRecommend($query, $self_id){
64
+ return $query->where('id', '!=', $self_id)->latest()->limit(3);
65
+ }
66
+
67
+ public function likes(){
68
+ return $this->hasMany('App\Like');
69
+ }
70
+ // $this->belongsToMany('モデル名', 中間テーブル名);
71
+ public function likePosts(){
72
+ return $this->belongsToMany('App\Post', 'likes');
73
+ }
74
+
75
+ // 自分がフォローしているユーザーを取得
76
+ public function follows(){
77
+ return $this->hasMany('App\Follow');
78
+ }
79
+
80
+ //自分がフォローしているユーザー
81
+ // $this->belongsToMany(モデル名, 中間テーブル名, リレーションを設定する側のカラム名, リレーションを設定される側のカラム名)
82
+ public function follow_users(){
83
+ return $this->belongsToMany('App\User', 'follows', 'user_id', 'follow_id');
84
+ }
85
+
86
+ // 自分をフォローしているユーザー
87
+ public function followers(){
88
+ return $this->belongsToMany('App\User', 'follows', 'follow_id', 'user_id');
89
+ }
90
+
91
+ public function isFollowing($user){
92
+ $result = $this->follow_users->pluck('id')->contains($user->id);
93
+ return $result;
94
+ }
95
+ }
96
+
97
+ ```
98
+
99
+ ```
18
100
  LikeController.php(いいね一覧のコントローラーが書かれた部分)
19
101
  <?php
20
102