【问题标题】:Laravel merge relationship of multiple models into one queryLaravel 将多个模型的关系合并到一个查询中
【发布时间】: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


【解决方案1】:

您需要定义Userbacheca 之间的关系。类似这样的方法可能有效,但您可能需要更改外键和/或本地键以适合您的数据库架构:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 2016-01-26
    • 2018-04-15
    • 2021-08-27
    • 2017-11-04
    • 1970-01-01
    • 2015-02-02
    • 2018-12-11
    相关资源
    最近更新 更多