0
所以我有一个像Twitter的应用程序,但我想“墙”有来自所有用户,但以当前用户的利益被过滤的帖子...通过用户兴趣过滤帖子?
我想要的东西,像下面这样:
$posts = PostModel::with('interests')->latest()->get();
$user = UserModel::find(Auth::user()->id);
foreach ($posts as $post) {
foreach($user->interests as $interest){
return only the posts that have the interest_id that matches
the user's interest_ids
}
}
这是我的usermodel利益的功能:
public function interests() {
return $this->belongsToMany('InterestModel', 'users_interests', 'user_id', 'interest_id')->withPivot('interest_id');
}
这是我的兴趣PostModel功能:
public function interests() {
return $this->belongsToMany("InterestModel", 'post_interest', 'post_id', 'interest_id');
}
我只是不知道如何排序和返回帖子?
感谢
马特