doulingna9420 2018-07-26 09:54
浏览 49

Laravel多次加入?

I'm trying to take out admin username by leftJoin. I have a table called server_bans_history and users.

server_bans_history table have a unique row called steamid and admin_steamid

taking all data by:

@foreach ($user->BanHistory as $BanHistory)

This code works fine, but i dont know hot got admin_steamid row and i need convert that steamid to username from users table.

Becouse now if i use {{ $BanHistory->username }} i'm getting BANNED user USERNAME not ADMIN username.

Example now i'm using code:

$user->BanHistory = DB::table('server_bans_history AS sh')
->leftJoin('users AS us', 'us.steam_id', '=', 'sh.steamid')
->where('sh.steamid', '=', $user->steam_id)
->select("sh.*", "us.*")
->orderBy('sh.date', 'desc')
->get('');

I need to convert admin_steamid -> user.username

Can i do with multiple leftJoin or something like that?

Thanks for helping me uderstand leftJoin!

  • 写回答

1条回答 默认 最新

  • douyouyi8878 2018-07-26 10:40
    关注

    I think this should be it:

    $user->BanHistory = DB::table('server_bans_history AS sh')
    ->leftJoin('users AS us', 'us.steam_id', '=', 'sh.steamid')
    ->leftJoin('users AS us2', 'us.steam_id', '=', 'sh.admin_steamid')
    ->where('sh.steamid', '=', $user->steam_id)
    ->select("sh.*", "us.*", "us2.username as admin_name")
    ->orderBy('sh.date', 'desc')
    ->get('');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加