连接表并找到并获取指定的行 [英] join table and find and get an specified row laravel

查看:116
本文介绍了连接表并找到并获取指定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从联接表中获取指定的记录,我的查询如下

Im trying to get an specified record from a join table, my query is as below

$user = Auth::user(); //current authenticated user
$user_id = $user->id; //get the current authenticated user's id
$users = DB::table('users') //join table users and table user_details base from matched id;
    ->join('user_details', 'users.id', '=', 'user_details.id')
    ->find($user->id) //find the record matched to the current authenticated user's id from the joint table records
    ->get(); //get the record
dd(var_dump($users)); //dump result

但不幸的是,它给了我这个错误

but unfortunately sadly it gives me this error

SQLSTATE [23000]:违反完整性约束:1052 where子句中的列'id'不明确(SQL:从users上的users内部联接user_details中选择*.id = user_details.id,其中id = 1个限制1)

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from users inner join user_details on users.id = user_details.id where id = 1 limit 1)

所以我敢打赌我的查询是错误的大声笑!无论如何,有什么帮助,想法吗?

so i bet my query is wrong LOL! anyway, any help, ideas please?

推荐答案

由于错误本身描述了id应该在哪些表上放置条件,因此您需要将find替换为where,例如

As the error itself depicts that on which tables id should it place condition so you need to replace your find with where like as

->where("users.id",$user->id)

代替

->find($user->id)

这篇关于连接表并找到并获取指定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆