前提
PHP Laravelでフォロー一覧を作っています。
フォローしたユーザーをprofile/myfollowに表示し、そこに表示されたユーザーをクリックするとそのユーザーのプロフィールに飛べるようにしたいです。今ProfileController.phpのpublic function followings()でフォローユーザーの取得をする関数を定義しようとしています。中で全フォローを$followingsと定義し、profile/myfollowでforeachで各フォローユーザーごとに表示しようと思います。
そこでfollowersテーブルに'following_id'と'followed_id'カラムがあるのですが、
「ログインユーザーidとfollowing_id'が一致→そのidに対応する'followed_id'を全て取得」
を$followingsで定義したいのです。しかしどのように書けば良いのかよく分かりません。
実現したいこと
「ログインユーザーidとfollowing_id'が一致→そのidに対応する'followed_id'を全て取得」
を$followingsで定義したいのです。
発生している問題・エラーメッセージ
Method Illuminate\Database\Eloquent\Collection::paginate does not exist. public function __call($method, $parameters) { if (! static::hasMacro($method)) { throw new BadMethodCallException(sprintf( 'Method %s::%s does not exist.', static::class, $method )); }
該当のソースコード
migrations\Y_m_d_t_create_followers_table.php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('followers', function (Blueprint $table) { 17 $table->unsignedInteger('following_id')->comment('フォローしているユーザID'); 18 $table->unsignedInteger('followed_id')->comment('フォローされているユーザID'); 19 20 $table->index('following_id'); 21 $table->index('followed_id'); 22 23 $table->unique([ 24 'following_id', 25 'followed_id' 26 ]); 27 }); 28 } 29 30 /** 31 * Reverse the migrations. 32 * 33 * @return void 34 */ 35 public function down() 36 { 37 Schema::dropIfExists('followers'); 38 } 39};
User.php
1 public function followers() 2 { 3 return $this->belongsToMany(User::class, 'followers', 'followed_id', 'following_id'); 4 } 5 public function follows() 6 { 7 return $this->belongsToMany(User::class, 'followers', 'following_id', 'followed_id'); 8 } 9 10 public function follow( $user_id) 11 { 12 return $this->follows()->attach($user_id); 13 } 14 public function unfollow( $user_id) 15 { 16 return $this->follows()->detach($user_id); 17 } 18 public function isFollowing( $user_id) 19 { 20 return (boolean) $this->follows()->where('followed_id', $user_id)->first(['id']); 21 } 22 public function isFollowed( $user_id) 23 { 24 return (boolean) $this->followers()->where('following_id', $user_id)->first(['id']);
Follower.php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8 9class Follower extends Model 10{ 11 use HasFactory; 12 13 protected $primaryKey = [ 14 'following_id', 15 'followed_id' 16 ]; 17 protected $fillable = [ 18 'following_id', 19 'followed_id' 20 ]; 21 public $timestamps = false; 22 public $incrementing = false; 23}
ProfileController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\User; 6use App\Models\Role; 7use App\Models\Follower; 8use Illuminate\Http\Request; 9use Illuminate\Support\Facades\Hash; 10use Illuminate\Validation\Rule; 11use Illuminate\Support\Facades\Storage; 12use Illuminate\Pagination\Paginator; 13 14class ProfileController extends Controller 15{ 16 public function followings(User $user) 17 { 18 $follower = auth()->user(); 19 $followings = Follower::where('following_id', $follower->id)->get(); 20 21 return view('profile.myfollow', compact('followings')); 22 } 23}
profile.myfollow.blade.php
1<x-app-layout> 2 <x-slot name="header"> 3 <h2> 4 フォローしたユーザー 5 </h2> 6 7 <x-message :message="session('message')" /> 8 9 </x-slot> 10 11 {{-- 投稿一覧表示用のコード --}} 12 @if ($followings->count() == 0) 13 <p class="mt-4"> 14 まだフォローをしていません。 15 </p> 16 @else 17 <div> 18 @foreach($followings as $following) 19 20 <div> 21 <div> 22 <div> 23 <div> 24 <a href="{{route('profile.show',$following)}}"> 25 <div> 26 <div> 27 <img src="{{asset('storage/avatar/'.($following->avatar??'user_default.jpg'))}}"> 28 </div> 29 <div>{{ $following->name??'削除されたユーザー' }}</div> 30 </div> 31 </a> 32 </div> 33 </div> 34 </div> 35 </div> 36 @endforeach 37 </div> 38 @endif 39 {{ $followings->links('vendor.pagination.tailwind2') }} 40</x-app-layout>
試したこと
bladeファイルの下部にlinks()とあることからoffsetしてかつlimitしようと思い$followings = $user->follows()->paginate(5);
としてみたところ、bladeファイルは見えました。しかしこれだけではbladeのcount()が機能されていないと思い、
if ($followings->count() == 0)
を!=としたりそもそもifを外してみました。app-blade以外は何も表示されていないので、count()==0となっているわけではありません(実際に幾つかフォローしフォローされているので0ではありません)。多分follows()の中の引数を設定したり→に続けて記述したりする必要があるのかもしれないと思い、Eloquentモデルなどを調べてみましたがよく分かりません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。