dougua9165 2019-04-05 21:28
浏览 172

如何使用Laravel显示具有匹配记录字段的用户? [重复]

This question already has an answer here:

Display the users with matching ID fields

staff details should only return when staffID(employer table) == id(staff table)

Below is my database schema

Staff('id' , 'username' , 'location');

Employer('id' , 'employerID','staffID' );

Below is what I have tried , however it is not return anything , i also have it returning to my blade file and a @foreach loop to get the data. I've tried have a model methods but no luck.

   $following =  DB::table('staff')
    ->select('username')
   ->join('employer' , 'employer.staffID', '=', 'staff.id')
    ->where('employer.employer_id' , '=', Auth::id())
    ->where('employer.staffID' ,'=', 'staff.id') 
    ->get(); 

below is how I am trying to get my querys results to display in my blade

 @foreach ($following as $userFound)

                        <p> {{ $userFound->username}}</p>                 

 @endforeach   
</div>
  • 写回答

1条回答

  • du13932014807 2019-04-05 22:05
    关注

    with raw SQL

    $query = "SELECT username 
              FROM employer JOIN staff ON employee.staffID = staff.id
              WHERE employerID = ?";
    $params = [Auth::id()];
    
    $following = \DB::select($query , $params);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题