WST87448735
@WST87448735

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[Laravel]自分とフォローした人の投稿を取得して表示する

解決したいこと

レシピサイトを作っています
自分かつフォローしている人の投稿を取得してタイムラインに表示する。

該当するソースコード

RecipeController
public function index()
    {
        $user = \Auth::user();
        $recipes = Recipe::all();
        foreach($recipes as $recipe){
        $data = Carbon::createFromFormat('Y-m-d H:i:s',$recipe->created_at)->format('Y年m月d日');
        }

        return view('recipes.index',[
           'title' => 'レシピ一覧', 
           'user' => $user,
           'recipes' => $user->recipes()->latest()->get(),
           'recommended_users' => User::recommend($user->id)->get(),
           'data' => $data,
        ]);
    }

フォローについてのリレーション関係

User.php
 public function follows()
    {
        return $this->hasMany('App\Follow');
    }
    
    public function follow_users()
    {
        return $this->belongsToMany('App\User','follows','user_id','follow_id');
    }
    
    public function followers()
    {
        return $this->belongsToMany('App\User','follows','follow_id','user_id');
    }
    
    public function isFollowing($user)
    {
        $result = $this->follow_users->pluck('id')->contains($user->id);
        return $result;
    }
    
    public function scopeRecommend($query,$self_id)
    {
        return $query->where('id','!=',$self_id)->latest()->limit(3);
    }
    
0

No Answers yet.

Your answer might help someone💌