因此,用户可以关注多个人,他们可以创建多个帖子,我正在变形我的帖子,因为多个项目可以创建帖子,
所以,我与用户的时间轴关系是这样的:
/**
* Get the owner.
*
* @return
* App\Modules\Account\Entites\User
* App\Modules\Groups\Entites\Groups
* App\Modules\Pages\Entites\Pages
* App\Modules\Events\Entites\Events
*/
public function owner()
{
return $this->morphTo('owner');;
}
时间表:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('timeline', function (Blueprint $table) {
$table->increments('id');
$table->text('status')->nullable();
$table->string('location')->nullable();
$table->string('feeling')->nullable();
$table->string('type')->nullable();
$table->string('privacy')->default('public');
$table->integer('owner_id');
$table->string('owner_type');
$table->timestamps();
$table->softDeletes();
});
}
现在,我的用户可以关注其他用户和网页,这样也正在变形:
/**
* Return item followings.
*
* @param string $class
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followings($class = __CLASS__)
{
return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
->withPivot('followable_type', 'relation', 'created_at');
}
/**
* Return followers.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followers()
{
return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
->withPivot('followable_type', 'relation', 'created_at');
}
以下结构:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('follow.followable_table', 'followables'), function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->unsignedInteger('followable_id');
$table->string('followable_type')->index();
$table->string('relation')->default('follow')->comment('folllow/like/subscribe/favorite/');
$table->timestamp('created_at');
$table->foreign('user_id')
->references(config('follow.users_table_primary_key', 'id'))
->on(config('follow.users_table_name', 'users'))
->onUpdate('cascade')
->onDelete('cascade');
});
}
现在,我正在努力获取我所关注的人的状态(帖子):
示例数据;
INSERT INTO `social`.`timeline`
(
`id`,
`status`,
`location`,
`feeling`,
`type`,
`privacy`,
`owner_id`,
`owner_type`,
`created_at`,
`updated_at`,
`deleted_at`
)
VALUES
(
'2',
'hellov',
NULL,
NULL,
NULL,
'public',
'1',
'App\\Modules\\Account\\Entities\\User',
'2018-02-05 20:02:08',
'2018-02-05 20:02:08',
NULL
);
所有者类型可以是多个(检查最顶层的所有者方法)我的查询
$statuses = $this
->timeline
->whereHas('owner.followers', function ($q) use ($user) {
return $q->where('id', Auth::user()->id);
})
// ->whereHas('owner.likes', function ($q) use ($user) {
// return $q->where('id', Auth::user()->id);
// })
->orderBy('timeline.created_at', 'desc')
->get();
但这会引发错误:
调用未定义的方法 照亮\数据库\查询\生成器::跟随者()
所以,我的问题是,当这种关系存在时,我怎么会得到错误,我该如何解决呢?
我已经构建了一个查询,因此您可以更好地理解我的逻辑
/**
* Scope a query to friends and followers statuses.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFriendsAndFollowers($query)
{
$query->leftJoin('followables', function ($join) {
$join
->on('timeline.owner_id', '=', 'followables.followable_id')
->on('timeline.owner_type', '=', 'followables.followable_type');
})
->leftJoin('friendships', function ($join) {
$join
->on('timeline.owner_id', '=', 'friendships.recipient_id');
})
->where(function($query) {
$query
->where('followables.user_id', Auth::user()->id, 'and')
->where('privacy', 'public')
->orWhere('privacy', 'private');
})
->Orwhere(function($query) {
$query
->where(function($query) {
$query
->where('friendships.sender_id', Auth::user()->id)
->orWhere('friendships.recipient_id', Auth::user()->id);
})
->where('privacy', 'public')
->orWhere('privacy', 'private');
})
->orWhere(function ($query) {
$query
->where('timeline.owner_id', Auth::user()->id)
->where('timeline.owner_type', get_class(Auth::user()));
});
}