我有兩個模型,用戶和枚舉器。我想搜索枚舉器模型中的某些列及其在用戶模型中的關系。這就是我所擁有的;枚舉器唯一身份用戶名我想編寫一個查詢來獲取同一集合中的 unique_id 和 first_name 。這就是我所擁有的;Enumerator::with(['user' => function($query) {
$query->select('id', 'first_name', 'last_name', 'email');
}])->get(['first_name', 'unique_id']);我該怎么辦?
1 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
如果您想在同一集合中獲取多個表列,最好在此處使用聯接查詢,如下所示
$joinTableName = (new App\User())->getTable();
$fromTableName = (new App\Enumerator())->getTable();
$foreignKey = "enumerators_id"; //user table set foreign key
$localKey = "id";? //enumerators table column local key
$selectColumns = [
? ? "{$joinTableName}.first_name",
? ? "{$fromTableName}.unique_id",
];
$a = App\Enumerator::select($selectColumns)
? ? ->join(
? ? ? ? $joinTableName,
? ? ? ? "{$joinTableName}.{$foreignKey}",
? ? ? ? '=',
? ? ? ? "{$fromTableName}.{$localKey}"
)->get();
dd($a);
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報
0/150
提交
取消