简体   繁体   English

错误:违反完整性约束:加入 Laravel Eloquent 时出现 1052

[英]Error: Integrity constraint violation: 1052 when Join in Laravel Eloquent

I am trying to join two table together by specifying their relationship (ie foreign key and local key) and using find(id).我试图通过指定它们的关系(即外键和本地键)并使用 find(id) 将两个表连接在一起。 Ab initio, I used where and get().从头开始,我使用了 where 和 get()。 It didn't gave the same error then I commented out the where clause to use find($id)它没有给出同样的错误然后我注释掉了 where 子句以使用 find($id)

"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select `votes`.`flavour`, `users`.`email` from `votes` inner join `users` on `votes`.`user_id` = `users`.`id` and `id` = 1 limit 1)",

This is the function for displaying based on the selection public function show($id) {这是基于选择显示的函数 public function show($id) {

         $dbconnect = DB::table('votes')
         ->join('users', 'votes.user_id', '=', 'users.id')
         //->where('votes.id', $id)
         ->select('votes.id','votes.date_of_birth','votes.voter_ip','votes.flavour', 'users.email')
         ->find($id);
         $vote = auth()->user()->$dbconnect;
        if (!$vote) {
            return response()->json([
                'success' => false,
                'message' => 'Vote with id ' . $id . ' not found'
            ], 400);
        }

        return response()->json([
            'success' => true,
            'data' => $vote->toArray()
        ], 400);
    }

Show all: in this function I am supposed to use join as well but I only used votes table it displayed accurately I need to join the votes and users table on votes.user_id = users.id全部显示:在这个函数中,我也应该使用 join,但我只使用了它准确显示的投票表 我需要在 votes.user_id = users.id 上加入投票和用户表

public function index()
        {
            $votes = auth()->user()->votes;

            return response()->json([
                'success' => true,
                'data' => $votes
            ]);
        }

Thank you谢谢

It's because you are using the ->find($id) function.这是因为您正在使用->find($id)函数。 This function will look for the primary key of the model (in this case id ) and use this as the filter.此函数将查找模型的主键(在本例中为id )并将其用作过滤器。 However, because of the join, this field is ambiguous.但是,由于join,这个字段是有歧义的。

To solve this, you can add the filter manually:要解决此问题,您可以手动添加过滤器:

$dbconnect = DB::table('votes')
     ->join('users', 'votes.user_id', '=', 'users.id')
     ->select('votes.id','votes.date_of_birth','votes.voter_ip','votes.flavour', 'users.email')
     ->where('votes.id', $id)
     ->first();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [23000]:完整性约束违规:1052. Laravel eloquent 连接表时出现问题 - SQLSTATE[23000]: Integrity constraint violation: 1052. Laravel eloquent issue when joining table SQLSTATE [23000]:违反完整性约束:1052与laravel结合使用 - SQLSTATE[23000]: Integrity constraint violation: 1052 using join with laravel 违反完整性约束:1052 - Integrity constraint violation: 1052 Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 5.6保存测验记录时违反口才完整性约束 - Laravel 5.6 Eloquent integrity constraint violation when saving quiz record Laravel雄辩的关系-违反完整性约束 - Laravel Eloquent Relationships - Integrity constraint violation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM