连接表并找到并获取指定的行 [英] join table and find and get an specified row laravel
问题描述
我试图从联接表中获取指定的记录,我的查询如下
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 joinuser_details
onusers
.id
=user_details
.id
whereid
= 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屋!