実現したいこと
投稿一覧画面で自分の投稿とフォローしたユーザーの投稿を表示させたい。
前提
PHP, Laravelでブログサイトを作っています。
フォロー機能を作成後、投稿一覧画面でフォローしたユーザーの投稿を表示させたいと思いコードを書いたのですが、フォローしていないユーザーの投稿が表示されてどこを間違えているのか分からないので教えていただけると幸いです。
該当のソースコード
show.blade.php
php
1@extends('layouts.logged_in') 2 3@section('title', $title) 4 5@section('content') 6 <h1>{{ $title }}</h1> 7 <ul class="recommend_users"> 8 <li> 9 <a href="{{ route('users.show', $recommended_user) }}">{{ optional($recommended_user)->name }}</a> 10 @if(Auth::user()->isFollowing($recommended_user)) 11 <form method="post" action="{{route('follows.destroy', $recommended_user)}}" class="follow"> 12 @csrf 13 @method('delete') 14 <input type="submit" value="フォロー解除"> 15 </form> 16 @else 17 <form method="post" action="{{route('follows.store')}}" class="follow"> 18 @csrf 19 <input type="hidden" name="follow_id" value="{{ $recommended_user->id }}"> 20 <input type="submit" value="フォロー"> 21 </form> 22 @endif 23 </li> 24 </ul> 25 @forelse($posts as $post) 26 <li> 27 {{ $post->user->name }}: 28 {!! nl2br($post->comment) !!}<br> 29 ({{ $post->created_at }}) 30 </li> 31 @empty 32 <p>投稿がありません。</p> 33 @endforelse 34@endsection
UserController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\User; 7use App\Http\Requests\UserRequest; 8use App\Http\Requests\UserImageRequest; 9 10class UserController extends Controller 11{ 12 public function __construct() 13 { 14 $this->middleware('auth'); 15 } 16 17 public function show($id){ 18 $user = User::find($id); 19 $posts = $user->posts()->latest()->get(); 20 21 return view('users.show',[ 22 'title' => 'プロフィール', 23 'user' => $user, 24 'posts' => $posts, 25 'recommended_user' => User::recommend($user->id)->get()->first(), 26 ]); 27 } 28 29 public function edit() { 30 $user = \Auth::user(); 31 32 return view('users.edit', [ 33 'title' => 'プロフィール編集', 34 'user' => $user, 35 ]); 36 } 37 38 public function update(UserRequest $request) { 39 $user = \Auth::user(); 40 $user->update($request->only(['name', 'email', 'profile'])); 41 42 session()->flash('success', 'プロフィールを編集しました'); 43 return redirect()->route('users.show', $user); 44 45 } 46}
User.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Foundation\Auth\User as Authenticatable; 7use Illuminate\Notifications\Notifiable; 8use App\Follow; 9 10class User extends Authenticatable 11{ 12 use Notifiable; 13 14 15 protected $fillable = [ 16 'name', 'email', 'password', 'profile', 17 ]; 18 19 protected $hidden = [ 20 'password', 'remember_token', 21 ]; 22 23 protected $casts = [ 24 'email_verified_at' => 'datetime', 25 ]; 26 27 //リレーションを設定 28 public function posts(){ 29 return $this->hasMany('App\Post'); 30 } 31 public function isEditable($post){ 32 return $this->isAdmin() || $this->id === $post->user->id; 33 } 34 35 public function isAdmin(){ 36 return $this->admin === 1; 37 } 38 39 public function scopeRecommend($query, $self_id){ 40 return $query->where('id', '!=', $self_id)->latest()->limit(3); 41 } 42 43 public function follows(){ 44 return $this->hasMany('App\Follow'); 45 } 46 47 public function follow_users(){ 48 return $this->belongsToMany('App\User', 'follows', 'user_id', 'follow_id'); 49 } 50 51 public function followers(){ 52 return $this->belongsToMany('App\User', 'follows', 'follow_id', 'user_id'); 53 } 54 55 public function isFollowing($user){ 56 $result = $this->follow_users->pluck('id')->contains($user->id); 57 return $result; 58 } 59}
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
testuser1でログインし、testuser2をフォローしました。
投稿一覧にはtestuser1の投稿とtestuser3の投稿が表示されてしまいました。
testuser1とtestuser2の投稿を表示させたいです。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
退会済みユーザー
2023/04/05 08:16
2023/04/05 08:19
退会済みユーザー
2023/04/05 08:45 編集
2023/04/05 08:59
退会済みユーザー
2023/04/05 09:02
2023/04/05 09:06
退会済みユーザー
2023/04/05 10:33
2023/04/05 10:58
退会済みユーザー
2023/04/05 12:51