【问题标题】:how to join 3 table in laravel using database mongodb?如何使用数据库 mongodb 在 laravel 中加入 3 个表?
【发布时间】:2018-01-19 12:23:01
【问题描述】:

我们的应用程序有 3 个模型“Notification”、“Postlike”和“Post”

在通知模型中:

public function postlike()
{
    return $this->belongsTo('App\Models\PostLikes', 'postlist');
}
public function post()
{
    return $this->hasMany('App\Models\PostLikes', '_id', 'c_post_id');
}

在 Postlike 模型中:

public function postlist()
{
    return $this->hasMany('App\Models\Post', '_id', 'c_post_id');
}

在通知存储库中:(查询)

public function getnotification($userId)
{
     $notification = $this->makeModel()
     ->with('post') 
     ->with('postlike')
     ->with('notification')
      ->orderBy('created_at' , 'desc')
      ->where('replied_id', $userId)    
     ->get();

     return $notification;
}

欲了解更多信息,我们附上了以下图片 enter image description here

【问题讨论】:

    标签: mongodb laravel join


    【解决方案1】:

    通知模型:

    public function postlike()
    {
        return $this->belongsTo('App\Models\PostLikes', 'c_post_id');
    }
    
    Query:
    
    $notification = $this->makeModel()
    {
        ->with('post', 'postlike', 'postlike.postlist')->get()
    }
    

    【讨论】:

      【解决方案2】:

      当我第一次看到你的帖子时,我认为你的问题可以使用 Laravel 的内置 eager loading 来解决,所以它可以让你做这样的事情:

      $notification = $this->makeModel()
           ->with('post', 'postlike', 'postlike.postlist')
           //...
           ->get();
      

      如果这是你的情况,这些链接应该可以解决问题:

      https://laracasts.com/discuss/channels/eloquent/eager-load-multiple-nested-relationships

      laravel eloquent eager load multiple nested relationships

      我不确定,但对我来说似乎有点奇怪,“通知”模型与“Postlike”模型有两个关系。也许您应该考虑使用一些数据透视表。

      希望对你有帮助,

      【讨论】:

        猜你喜欢
        • 2019-10-03
        • 2019-10-06
        • 2012-07-04
        • 2016-07-26
        • 2021-04-02
        • 2020-11-22
        • 2022-01-10
        • 1970-01-01
        相关资源
        最近更新 更多