在我的网站中,我有一张Users
的表格。 Users
可以互相关注。用户可以创建任意数量的Post
。
我希望能够看到我所关注的用户中最新的Post
。
目前我的模型定义如下:
用户模型:
class User extends Authenticatable
{
public function followers(){
return $this->hasMany('App\Follower', 'following');
}
public function following(){
return $this->hasMany('App\Follower', 'id');
}
public function posts(){
return $this->hasMany('App\Post', 'createdby');
}
}
关注者模型:
class Follower extends Model
{
public function postsFollowing(){
return $this->hasMany('App\Post', 'createdby', 'following');
}
}
发布模型:
class Post extends Model
{
public function user(){
return $this->belongsTo('App\User', 'createdby', 'id');
}
}
我的表是这样的:
Table Name, column names
User id, name
Follower id, following
Post id, created_by
在Follower
表格中,id
代表用户,following
代表被关注的用户。如果用户3跟随用户537,则id = 3
,following = 537
。希望这是有道理的。
我尝试过的事情:
User::following()->posts
- 没有用,因为
User::following()
返回一个Eloquent Collection对象。你有
循环使用
通过我的关注用户循环获取他们的Post
' s
因为我想要排序前n个条目,所以不起作用
日期。
更新#1
追随者模型(更新)
class Follower extends Model
{
public function followingPosts(){
return $this->hasManyThrough(
'App\Post', 'App\Follower',
'following', 'createdby', 'id'
);
}
}
控制器
$user = Auth::user();
$posts = $user->followingPosts;
我使用上述内容更新了Follower类中的followingPosts()。结果:$posts
为null
更新#2
我将以下Post()移动到用户模型:
public function followingPosts(){
return $this->hasManyThrough(
'App\Post', 'App\Follower',
'following', 'createdby'
);
}
控制器:
$user = Auth::user();
$posts = $user->followingPosts;
现在我只收到所有帖子,即使是我没有关注的用户。
答案 0 :(得分:2)
您的要求 - "用户可以互相关注。用户可以创建任意数量的帖子。能够列出最近的帖子(限于数量)的关注者或用户关注的"。
您可以在用户模型上定义多对多关系(自我 - 用户模型上的多对多关系)。
创建两个数据透视表
关注者 - 用户数据透视表
class CreateFollowerUserPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('follower_user', function (Blueprint $table) {
$table->integer('follower_id')->unsigned();
$table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
$table->primary(['follower_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('follower_user');
}
}
以下用户数据透视表
class CreateFollowingUserPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('following_user', function (Blueprint $table) {
$table->integer('following_id')->unsigned();
$table->foreign('following_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
$table->primary(['following_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('following_user');
}
}
然后在用户模型中定义关系
class User extends Model
{
public function followers()
{
return $this->belongsToMany(User::class, 'follower_user', 'user_id', 'follower_id')->withTimestamps();
}
public function following()
{
return $this->belongsToMany(User::class, 'following_user', 'following_id', 'user_id' )->withTimestamps();
}
//Assuming posts table has user_id as foreign key
public function posts()
{
return $this->hasMany(Post::class);
}
//Assuming posts table has user_id as foreign key
public function recent_posts()
{
return $this->hasMany(Post::class)->take(10)->orderBy('created_at', 'desc');
}
}
现在要了解给定用户的关注
//Say for example we take the logged in user
$user = User::with('following.recent_posts')->whereEmail(auth()->user()->email);
foreach($user->following as $following)
{
$posts = $following->recent_posts;
}
希望这是你想要完成的事情。
答案 1 :(得分:1)
您可以将has-many-through
用作:
public function followingPosts()
{
return $this->hasManyThrough(
'App\Post', 'App\Follower',
'follow', 'createdby', 'id'
);
}
然后您可以访问帖子:
$user->followingPosts; // returns collection of post model
注意:假设follow
表中有Follower
列。