Laravel 将多个模型的关系合并到一个查询中

Laravel merge relationship of multiple models into one query

我想将来自 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();

或者你有更好的方法来合并这两个关系吗?

我想查看帖子内的评论总是尊重关系“relazioneFollower”的约束,非常感谢

您需要定义 Userbacheca 之间的关系。这样的事情可能会起作用,但您可能需要更改外部 and/or 本地键以适合您的数据库模式:

class User extends Authenticatable {

    public function relazioneFollower() {
        return $this->belongsToMany(User::class, 'user_follower', 'follower_id', 'following_id')->withTimestamps();
    }

    public function bachecas() {
        return $this->hasMany(bacheca::class, 'following_id', 'id_utente');
    }
}

然后在检索帖子时,它应该像使用 with() 一样简单,但需要注意的是 select() 方法。我不认为你可以从这种方法中获取关系字段。相反,您需要将其添加到 with() 方法中。这是完整的请求:

$posts = User::with([
        'relazioneFollower',
        'bacheca.commento:id_post,title,contenuto_post,file,immagine,video,audio,posizione,privacy,created_at,updated_at',
    ])

    ->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')
    
    ->get();