【发布时间】:2021-08-08 14:09:58
【问题描述】:
我想将 2 个关系合并到一个来自 2 个不同模型的查询中
class User extends Authenticatable
{
public function relazioneFollower() {
return $this->belongsToMany(User::class, 'user_follower','follower_id','following_id')
->withTimestamps();
}
}
class bacheca extends Model
{
protected $primaryKey = 'id_post';
public function commento() {
return $this->hasMany(Comment::class,'id_post_commento');
}
}
这段代码工作正常,但我想添加评论报告,以与各种帖子相关联:
$posts = User::find($current_id)->relazioneFollower()
->join('bachecas', function ($join2) use ($current_id) {
$join2->on('following_id', '=', 'bachecas.id_utente')
->where('user_follower.stato',2)
->where('user_follower.follower_id', '=', $current_id);
})
->where(function ($filter) {
$filter->where('privacy', 1);
})
->orWhere(function ($filter) {
$filter->where('privacy', 2)
->where('relazione', 2)
->orWhere('relazione', 3);
})
->orWhere(function ($filter) {
$filter->where('privacy', 3)
->where('relazione', 3);
})
->orWhere(function ($filter) {
$filter->where('privacy', 5)
->where('relazione', 2)
->orWhere('relazione', 3)
->orWhere('relazione', 4);
})
->orderBy('created_at', 'desc')
->select('users.id', 'users.name', 'users.cognome', 'users.fotoProfilo','bachecas.id_post','bachecas.title','bachecas.contenuto_post','bachecas.file','bachecas.immagine','bachecas.video','bachecas.audio','bachecas.posizione','bachecas.privacy','bachecas.created_at','bachecas.updated_at')
->get();
有没有办法将主查询与这种关系合并?
$comment=bacheca::with('commento')->get();
或者你有更好的方法来合并这两种关系吗?
我想查看帖子中的 cmets 始终尊重“relazioneFollower”关系的约束,非常感谢您
【问题讨论】:
-
但是有人吗?还是我的要求太难了?
标签: laravel vue.js relationship