如何在热切的负载中链接2个或更多选择标签? 我使用select是因为我只想加载这2列,没有别的。尝试了很多事情。
$teams = Team::with(['captain' => function($q) {
$q->select('id', 'name')->where('show_name', true);
$q->select('id', 'username')->where('show_username', true);
}])
显然,第一个实现无法正常工作,但它显示了我正在尝试实现的目标:
$q->select('id', 'name', 'username')
->where('show_name', true)
->orWhere('show_username', true);
}])
如果name
或username
为show_name
,则第二个实现同时返回show_username
和true
。我不会这样:
$q->select('id', 'name')
->where('show_name', true)
->orWhere() // ??
->select('id', 'username')
->where('show_username', true);
}])
谢谢
答案 0 :(得分:0)
这是不可能的。
SQL查询结果必须是一个表,因此每一行必须具有相同的列。但是,您可以在列中有条件地输入真实的用户名或名称值,如下所示:
$teams = Team::with(['captain' => function($q) {
$q->select('id',
\DB::raw('IF(show_username, username, NULL) as username'),
\DB::raw('IF(show_name, name, NULL) as name')
);
}])