显示左连接查询数据[Laravel 5]

时间:2023-02-05 00:17:40

stackers I'm facing a problem on displaying data from query that contain join statement.

堆栈器我在查询中显示包含join语句的数据时遇到问题。

public function view(Request $id)
{
    $user_id = $id->input('id');
    $users = DB::table('users')->select('*')
                ->leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
                ->leftjoin('roles', 'roles.id', '=', 'role_user.role_id')
                ->where('users.id', '=', $user_id)->get();
    return view('/admin/view_user', ['users' => $users]);
}

From the above code, how do i display the row and the join table data from roles, and role_user

从上面的代码中,我如何显示角色和role_user中的行和连接表数据

1 个解决方案

#1


1  

Try This :

尝试这个 :

public function view(Request $id)
{
    $user_id = $id->input('id');
    $users = DB::table('users')
                ->select('users.*','role_user.fieldName','roles.fieldName')
                ->leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
                ->leftjoin('roles', 'roles.id', '=', 'role_user.role_id')
                ->where('users.id', '=', $user_id)->get();
    return view('/admin/view_user', ['users' => $users]);
}

Write tableName.fieldName to get perticalur table's data in JOIN query

编写tableName.fieldName以获取JOIN查询中的perticalur表数据

#1


1  

Try This :

尝试这个 :

public function view(Request $id)
{
    $user_id = $id->input('id');
    $users = DB::table('users')
                ->select('users.*','role_user.fieldName','roles.fieldName')
                ->leftjoin('role_user', 'users.id', '=', 'role_user.user_id')
                ->leftjoin('roles', 'roles.id', '=', 'role_user.role_id')
                ->where('users.id', '=', $user_id)->get();
    return view('/admin/view_user', ['users' => $users]);
}

Write tableName.fieldName to get perticalur table's data in JOIN query

编写tableName.fieldName以获取JOIN查询中的perticalur表数据