前提・実現したいこと
自分の投稿一覧の取得はできたのですが、それにフォローしてる人の投稿を含めたものの取得方法がわかりません。
MySQLで言うと下記のようなことをEloquentで行いたいです。
select * from comments join followings on comments.user_id = followings.following_user_id where followings.user_id = 任意のid order by comments.created_at desc;
該当のソースコード
Following.php
1namespace App\Models; 2 3use Illuminate\Database\Eloquent\Factories\HasFactory; 4use Illuminate\Database\Eloquent\Model; 5 6class Following extends Model 7{ 8 use HasFactory; 9 10 protected $fillable = [ 11 'user_id', 12 'following_user_id', 13 ]; 14 15 public function user() 16 { 17 return $this->belongsTo(User::class, 'user_id', 'id'); 18 } 19 20 public function comments() 21 { 22 return $this->hasMany(Comment::class); 23 } 24}
Comment.php
1namespace App\Models; 2 3use Illuminate\Database\Eloquent\Model; 4use App\Models\Article; 5use App\Models\User; 6 7class Comment extends Model 8{ 9 use HasFactory; 10 11 protected $fillable = [ 12 'body', 13 'user_id', 14 'article_id', 15 ]; 16 17 public function article() 18 { 19 return $this->belongsTo(Article::class); 20 } 21 22 public function user() 23 { 24 return $this->belongsTo(User::class); 25 } 26}
User.php
1namespace App\Models; 2 3use App\Models\Comment; 4 5class User extends Authenticatable 6{ 7 public function comments() 8 { 9 return $this->hasMany(Comment::class); 10 } 11 12 // フォローしてる人全て取得 13 // $follows = auth()->user()->follows()->get(); 14 public function follows() 15 { 16 return $this->belongsToMany(User::class, 'followings', 'user_id', 'following_user_id')->withTimestamps(); 17 } 18}
UserController.php
1namespace App\Http\Controllers; 2 3use Illuminate\Http\Request; 4use App\Models\User; 5use App\Models\Comment; 6use Auth; 7 8class UserController extends Controller 9{ 10 public function index() 11 { 12 $user = Auth::user(); 13 $comments = $user->follows->comments->whereId($user->id)->get(); 14 dd($comments); 15 exit; 16 // $comments = $user->comments()->latest()->get(); 17 18 return view('index', [ 19 'user' => $user, 20 'comments' => $comments, 21 ]); 22 } 23
commentsテーブル
1+------------+-----------------+------+-----+---------+----------------+ 2| Field | Type | Null | Key | Default | Extra | 3+------------+-----------------+------+-----+---------+----------------+ 4| id | bigint unsigned | NO | PRI | NULL | auto_increment | 5| user_id | bigint unsigned | NO | MUL | NULL | | 6| article_id | bigint unsigned | NO | MUL | NULL | | 7| body | text | YES | | NULL | | 8| created_at | timestamp | YES | | NULL | | 9| updated_at | timestamp | YES | | NULL | | 10+------------+-----------------+------+-----+---------+----------------+
followingsテーブル
1+-------------------+-----------------+------+-----+---------+----------------+ 2| Field | Type | Null | Key | Default | Extra | 3+-------------------+-----------------+------+-----+---------+----------------+ 4| id | bigint unsigned | NO | PRI | NULL | auto_increment | 5| user_id | bigint unsigned | NO | MUL | NULL | | 6| following_user_id | bigint unsigned | NO | MUL | NULL | | 7| created_at | timestamp | YES | | NULL | | 8| updated_at | timestamp | YES | | NULL | | 9+-------------------+-----------------+------+-----+---------+----------------+
```usersテーブル +-------------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO | UNI | NULL | | | email_verified_at | timestamp | YES | | NULL | | | password | varchar(255) | NO | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +-------------------+-----------------+------+-----+---------+----------------+
補足情報(FW/ツールのバージョンなど)
PHP 8.1.1
Laravel 8.79.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。