如何使用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个回答



with raw SQL </ p>

  $ query =“SELECT username 
FROM employer JOIN 员工ON employee.staffID = staff.id
WHERE employerID =?“;
$ params = [Auth :: id()];

$ following = \ DB :: select($ query,$ params) ;
</ code> </ pre>
</ div>

展开原文

原文

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);

dth20986
dth20986 大家好,一个简单的左连接就可以了。 不管怎么说,多谢拉
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐