【问题标题】:Adding multiple relations in eager loading在急切加载中添加多个关系
【发布时间】:2018-10-31 00:59:43
【问题描述】:

我的应用程序:在我的应用程序中,用户可以预测即将到来的足球比赛的比分。所以基本上userpredictions之间有关系,但prediction和我的Match model之间也有关系。目前我在我的预测表中添加了homeTeamNameawayTeamName,但这并不是必需的,因为我将match_id 存储在我的预测表中。我想根据我的预测表中的match_id 从我的match table 加载我的团队名称,而不是在预测表中添加名称。

这是我的关系:

匹配模型

class Match extends Model
{
    public function Predictions() {

        return $this->hasMany('App\Prediction'); // one match has many predictions
    }
}

预测模型

class Prediction extends Model
{
   public function User() {

       return $this->belongsTo('App\User'); // prediction belongs to a user
   }

   public function Match() {

       return $this->belongsTo('App\Match', 'match_id', 'match_id'); // prediction belongs to a match
   }
}

用户模型

class User extends Authenticatable
{
    public function Predictions() {

        return $this->hasMany('App\Prediction'); // a user has many predictions
    }

}

对此查询使用延迟加载

public function showPredictions() {
    \DB::enableQueryLog();
    $user = Auth::user();

    $user->load('predictions', 'predictions.match'); 

    dd(\DB::getQueryLog());

    return view('predictions', compact('user'));
}

输出

array:3 [▼
  0 => array:3 [▼
    "query" => "select * from `users` where `id` = ? limit 1"
    "bindings" => array:1 [▼
      0 => 1
    ]
    "time" => 13.0
  ]
  1 => array:3 [▼
    "query" => "select * from `predictions` where `predictions`.`user_id` in (?)"
    "bindings" => array:1 [▼
      0 => 1
    ]
    "time" => 1.0
  ]
  2 => array:3 [▼
    "query" => "select * from `matches` where `matches`.`id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    "bindings" => array:10 [▼
      0 => 233133
      1 => 233134
      2 => 233135
      3 => 233136
      4 => 233137
      5 => 233138
      6 => 233139
      7 => 233140
      8 => 233141
      9 => 233142
    ]
    "time" => 1.0
  ]
]

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    要急切加载嵌套关系,您可以使用“点”语法

    $user->load('predictions', 'predictions.match')->where('status', 'SCHEDULED'); 
    

    【讨论】:

    • 这似乎有效,但是当我 dd 这样做时,我得到以下信息:#relations: array:1 [▼ "match" => null ] 我不应该收到该 match_id 的所有列吗?
    • 它工作正常,我检查了。你能分享你的表转储吗? db-fiddle.com
    • db-fiddle.com/#&togetherjs=TzGJCfSnQ4这里是我的sql结构的链接
    • 我认为问题出在外键上。 ... public function Match() { return $this->belongsTo('App\Match', 'match_id'); // prediction belongs to a ...我的意思是你应该指定外键
    • 试试这个代码\DB::enableQueryLog(); $user->load('predictions', 'predictions.match')->where('status', 'SCHEDULED'); dd(\DB::getQueryLog());并分享输出
    【解决方案2】:

    试试这个

    $user->load([
       'predictions.match' => function ($query) {
            $query->where('status', 'SCHEDULED');
        }
    ]);
    

    或者,如果您在两个表中都有 status 列:

    $user->load([
       'predictions.match' => function ($query) {
            $query->where('predictions.status', 'SCHEDULED');
        }
    ]);
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2020-03-08
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多