質問編集履歴
2
コードの記載変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,130 +8,89 @@
|
|
8
8
|
フォロー機能を作成後、投稿一覧画面でフォローしたユーザーの投稿を表示させたいと思いコードを書いたのですが、フォローしていないユーザーの投稿が表示されてどこを間違えているのか分からないので教えていただけると幸いです。
|
9
9
|
|
10
10
|
### 該当のソースコード
|
11
|
-
|
11
|
+
show.blade.php
|
12
12
|
```php
|
13
13
|
@extends('layouts.logged_in')
|
14
|
-
|
14
|
+
|
15
15
|
@section('title', $title)
|
16
|
-
|
16
|
+
|
17
17
|
@section('content')
|
18
|
-
<h
|
18
|
+
<h1>{{ $title }}</h1>
|
19
19
|
<ul class="recommend_users">
|
20
|
-
|
20
|
+
<li>
|
21
|
-
<
|
21
|
+
<a href="{{ route('users.show', $recommended_user) }}">{{ optional($recommended_user)->name }}</a>
|
22
|
+
@if(Auth::user()->isFollowing($recommended_user))
|
23
|
+
<form method="post" action="{{route('follows.destroy', $recommended_user)}}" class="follow">
|
24
|
+
@csrf
|
25
|
+
@method('delete')
|
26
|
+
<input type="submit" value="フォロー解除">
|
27
|
+
</form>
|
28
|
+
@else
|
29
|
+
<form method="post" action="{{route('follows.store')}}" class="follow">
|
30
|
+
@csrf
|
31
|
+
<input type="hidden" name="follow_id" value="{{ $recommended_user->id }}">
|
32
|
+
<input type="submit" value="フォロー">
|
33
|
+
</form>
|
34
|
+
@endif
|
35
|
+
</li>
|
36
|
+
</ul>
|
37
|
+
@forelse($posts as $post)
|
38
|
+
<li>
|
39
|
+
{{ $post->user->name }}:
|
40
|
+
{!! nl2br($post->comment) !!}<br>
|
41
|
+
({{ $post->created_at }})
|
42
|
+
</li>
|
22
43
|
@empty
|
23
|
-
<
|
44
|
+
<p>投稿がありません。</p>
|
24
45
|
@endforelse
|
25
|
-
</ul>
|
26
|
-
<h1>{{ $title }}</h1>
|
27
|
-
<ul>
|
28
|
-
@forelse($posts as $post)
|
29
|
-
<li>
|
30
|
-
{{ $post->user->name }}:
|
31
|
-
{!! nl2br($post->comment) !!}<br>
|
32
|
-
({{ $post->created_at }})
|
33
|
-
@if($user->isEditable($post))
|
34
|
-
[<a href="{{ route('posts.edit', $post) }}">編集</a>]
|
35
|
-
@endif
|
36
|
-
<form action="{{ url('posts/'.$post->id) }}" method="post">
|
37
|
-
@csrf
|
38
|
-
@method('delete')
|
39
|
-
<button type="submit">削除</button>
|
40
|
-
</form>
|
41
|
-
</li>
|
42
|
-
@empty
|
43
|
-
<p>投稿がありません。</p>
|
44
|
-
@endforelse
|
45
|
-
</ul>
|
46
46
|
@endsection
|
47
47
|
```
|
48
|
-
|
48
|
+
UserController.php
|
49
49
|
```php
|
50
50
|
<?php
|
51
|
-
|
51
|
+
|
52
52
|
namespace App\Http\Controllers;
|
53
|
-
|
53
|
+
|
54
54
|
use Illuminate\Http\Request;
|
55
|
-
use App\Post;
|
56
|
-
use App\Http\Requests\PostRequest;
|
57
|
-
use App\Http\Requests\PostImageRequest;
|
58
55
|
use App\User;
|
59
|
-
|
56
|
+
use App\Http\Requests\UserRequest;
|
57
|
+
use App\Http\Requests\UserImageRequest;
|
58
|
+
|
60
|
-
class
|
59
|
+
class UserController extends Controller
|
61
60
|
{
|
62
|
-
// 投稿一覧
|
63
|
-
public function index(){
|
64
|
-
$user = \Auth::user();
|
65
|
-
$posts = $user->posts()->latest()->get();
|
66
|
-
$follow_user_ids = $user->follow_users->pluck('id');
|
67
|
-
$user_posts = $user->posts()->orWhereIn('user_id', $follow_user_ids )->latest()->get();
|
68
|
-
return view('posts.index', [
|
69
|
-
'title' => '投稿一覧',
|
70
|
-
'user' => $user,
|
71
|
-
'posts' => $user_posts,
|
72
|
-
'recommend_users' => User::recommend($user->id)->get()
|
73
|
-
]);
|
74
|
-
}
|
75
|
-
|
76
|
-
// 新規投稿フォーム
|
77
|
-
public function create()
|
78
|
-
{
|
79
|
-
return view('posts.create', [
|
80
|
-
'title' => '新規投稿',
|
81
|
-
]);
|
82
|
-
}
|
83
|
-
|
84
|
-
// 投稿追加処理
|
85
|
-
public function store(PostRequest $request){
|
86
|
-
Post::create([
|
87
|
-
'user_id' => \Auth::user()->id,
|
88
|
-
'comment' => $request->comment,
|
89
|
-
]);
|
90
|
-
session()->flash('success', '投稿を追加しました');
|
91
|
-
return redirect()->route('posts.index');
|
92
|
-
}
|
93
|
-
|
94
|
-
// 投稿詳細
|
95
|
-
public function show($id)
|
96
|
-
{
|
97
|
-
return view('posts.show', [
|
98
|
-
'title' => '投稿詳細',
|
99
|
-
]);
|
100
|
-
}
|
101
|
-
|
102
|
-
// 投稿編集フォーム
|
103
|
-
public function edit($id)
|
104
|
-
{
|
105
|
-
// ルーティングパラメータで渡されたidを利用してインスタンスを取得
|
106
|
-
$post = Post::find($id);
|
107
|
-
return view('posts.edit', [
|
108
|
-
'title' => '投稿編集',
|
109
|
-
'post' => $post,
|
110
|
-
]);
|
111
|
-
}
|
112
|
-
|
113
|
-
// 投稿更新処理
|
114
|
-
public function update($id, PostRequest $request)
|
115
|
-
{
|
116
|
-
$post = Post::find($id);
|
117
|
-
$post->update($request->only(['comment']));
|
118
|
-
session()->flash('success', '投稿を編集しました');
|
119
|
-
return redirect()->route('posts.index');
|
120
|
-
}
|
121
|
-
|
122
|
-
// 投稿削除処理
|
123
|
-
public function destroy($id)
|
124
|
-
{
|
125
|
-
$post = Post::find($id);
|
126
|
-
|
127
|
-
$post->delete();
|
128
|
-
session()->flash('success', '投稿を削除しました');
|
129
|
-
return redirect()->route('posts.index');
|
130
|
-
}
|
131
|
-
|
132
61
|
public function __construct()
|
133
62
|
{
|
134
63
|
$this->middleware('auth');
|
64
|
+
}
|
65
|
+
|
66
|
+
public function show($id){
|
67
|
+
$user = User::find($id);
|
68
|
+
$posts = $user->posts()->latest()->get();
|
69
|
+
|
70
|
+
return view('users.show',[
|
71
|
+
'title' => 'プロフィール',
|
72
|
+
'user' => $user,
|
73
|
+
'posts' => $posts,
|
74
|
+
'recommended_user' => User::recommend($user->id)->get()->first(),
|
75
|
+
]);
|
76
|
+
}
|
77
|
+
|
78
|
+
public function edit() {
|
79
|
+
$user = \Auth::user();
|
80
|
+
|
81
|
+
return view('users.edit', [
|
82
|
+
'title' => 'プロフィール編集',
|
83
|
+
'user' => $user,
|
84
|
+
]);
|
85
|
+
}
|
86
|
+
|
87
|
+
public function update(UserRequest $request) {
|
88
|
+
$user = \Auth::user();
|
89
|
+
$user->update($request->only(['name', 'email', 'profile']));
|
90
|
+
|
91
|
+
session()->flash('success', 'プロフィールを編集しました');
|
92
|
+
return redirect()->route('users.show', $user);
|
93
|
+
|
135
94
|
}
|
136
95
|
}
|
137
96
|
```
|
1
user.phpの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -135,6 +135,68 @@
|
|
135
135
|
}
|
136
136
|
}
|
137
137
|
```
|
138
|
+
User.php
|
139
|
+
```php
|
140
|
+
<?php
|
141
|
+
|
142
|
+
namespace App;
|
143
|
+
|
144
|
+
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
145
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
146
|
+
use Illuminate\Notifications\Notifiable;
|
147
|
+
use App\Follow;
|
148
|
+
|
149
|
+
class User extends Authenticatable
|
150
|
+
{
|
151
|
+
use Notifiable;
|
152
|
+
|
153
|
+
|
154
|
+
protected $fillable = [
|
155
|
+
'name', 'email', 'password', 'profile',
|
156
|
+
];
|
157
|
+
|
158
|
+
protected $hidden = [
|
159
|
+
'password', 'remember_token',
|
160
|
+
];
|
161
|
+
|
162
|
+
protected $casts = [
|
163
|
+
'email_verified_at' => 'datetime',
|
164
|
+
];
|
165
|
+
|
166
|
+
//リレーションを設定
|
167
|
+
public function posts(){
|
168
|
+
return $this->hasMany('App\Post');
|
169
|
+
}
|
170
|
+
public function isEditable($post){
|
171
|
+
return $this->isAdmin() || $this->id === $post->user->id;
|
172
|
+
}
|
173
|
+
|
174
|
+
public function isAdmin(){
|
175
|
+
return $this->admin === 1;
|
176
|
+
}
|
177
|
+
|
178
|
+
public function scopeRecommend($query, $self_id){
|
179
|
+
return $query->where('id', '!=', $self_id)->latest()->limit(3);
|
180
|
+
}
|
181
|
+
|
182
|
+
public function follows(){
|
183
|
+
return $this->hasMany('App\Follow');
|
184
|
+
}
|
185
|
+
|
186
|
+
public function follow_users(){
|
187
|
+
return $this->belongsToMany('App\User', 'follows', 'user_id', 'follow_id');
|
188
|
+
}
|
189
|
+
|
190
|
+
public function followers(){
|
191
|
+
return $this->belongsToMany('App\User', 'follows', 'follow_id', 'user_id');
|
192
|
+
}
|
193
|
+
|
194
|
+
public function isFollowing($user){
|
195
|
+
$result = $this->follow_users->pluck('id')->contains($user->id);
|
196
|
+
return $result;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
```
|
138
200
|
|
139
201
|
|
140
202
|
### 補足情報(FW/ツールのバージョンなど)
|