フォロー機能を作成を作りました。
自分以外のユーザーかつフォローした人以外という条件でユーザーを表示したいのですが、上手くいかず。
どうにか自分以外のユーザーは、取得できましたが、フォローした人以外を表示することが出来ませんでした。
ここが何度やっても上手くいかず。
//フォローしていない人
$unfollow = User::where('id' , '!=', $follow_user_ids)->get();
教えて頂けると幸いです。
BlogController <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Blog; use App\Http\Requests\BlogRequest; use App\User; use App\Follow; class BlogController extends Controller { public function index() { $user = \Auth::user(); // フォローしているユーザーのid $follow_user_ids = $user->follow_users->pluck('id'); // 条件:自分以外、フォローした人以外 //フォローしていない人 $unfollow = User::where('id' , '!=', $follow_user_ids)->get(); //自分以外のユーザー取得 $other_users = User::where('id' , '!=' , $user->id)->inRandomOrder()->limit(3)->get(); (省略) } Userモデル <?php namespace App; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; // 今回は、emailアドレスは不要 protected $fillable = [ 'name', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; protected $casts = [ 'email_verified_at' => 'datetime', ]; //リレーション public function blogs() { return $this->hasMany('App\Blog'); } //フォロー機能のリレーション 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; } }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/22 08:37