简体   繁体   English

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

I want to display some tables when user clicks id but its different tables, but I found error like this :我想在用户单击id但它的不同表时显示一些表,但我发现这样的错误:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous (SQL: select * from produk_usaha inner join 'perusahaan' on 'produk_usaha'.'id_perusahaan' = 'perusahaan'.'id_perusahaan' where 'id_perusahaan' = 134) SQLSTATE[23000]:违反完整性约束:1052 列 'id_perusahaan' 在 where 子句中是不明确的(SQL:select * from produk_usaha inner join 'perusahaan' on 'produk_usaha'.'id_perusahaan' = 'perusahaan'. ' = 134)

Can you help me to solve this ?你能帮我解决这个问题吗?

MyController我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MyController extends Controller
{
    public function detail($id)
    {
        $usaha = DB::table('perusahaan')
                        ->join('jenis_produk', 'perusahaan.id_jenis_produk', '=', 'jenis_produk.id_jenis_produk')
                        ->join('pengusaha', 'perusahaan.id_pengusaha', '=', 'pengusaha.id_pengusaha')
                        ->where('id_perusahaan', $id)->first();

        $produk = DB::table('produk_usaha')
                        ->join('perusahaan', 'produk_usaha.id_perusahaan', '=', 'perusahaan.id_perusahaan')
                        ->where('id_perusahaan', $id)->get();

        $loker = DB::table('lowongan_kerja')
                        ->join('perusahaan', 'lowongan_kerja.id_perusahaan', '=', 'perusahaan.id_perusahaan')
                        ->where('id_perusahaan', $id)->get();

        return view('detail', compact('usaha', 'produk', 'loker'));
    }
}

View (detail.blade)查看(detail.blade)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Test View</title>
    </head>
    <body>
        <h2>Info usaha</h2>
        <table border="1">
            <tr>
                <td width="100">Nama Usaha</td>
                <td width="10">:</td>
                <td>{{ $usaha->nama_perusahaan }}</td>
            </tr>
            <tr>
                <td>Jenis Produk</td>
                <td>:</td>
                <td>{{ $usaha->nama_jenis_produk }}</td>
            </tr>
            <tr>
                <td>Alamat Usaha</td>
                <td>:</td>
                <td>{{ $usaha->alamat_perusahaan }}</td>
            </tr>
            <tr>
                <td>Tanggal Didirikan</td>
                <td>:</td>
                <td>{{ $usaha->tanggal_didirikan }}</td>
            </tr>
            <tr>
                <td>No. Telp</td>
                <td>:</td>
                <td>{{ $usaha->no_telp }}</td>
            </tr>
            <tr>
                <td>Keterangan Usaha</td>
                <td>:</td>
                <td>{{ $usaha->keterangan_perusahaan }}</td>
            </tr>
        </table>

        <hr>

        <h2>Daftar Produk</h2>
        <table border="1">
            @foreach($produk as $p)
                <tr>
                    <td><img src="{{ asset('images/produk/'.$p->foto_produk_usaha) }}" alt=""></td>
                    <td>{{ $p->nama_produk_usaha }}</td>
                    <td>{{ $p->harga_produk_usaha }}</td>
                </tr>
            @endforeach
        </table>

        <hr>

        <h2>Lowongan Kerja</h2>
        <table border="1">
            @foreach($loker as $l)
                <tr>
                    <td>{{ $l->judul_loker }}</td>
                    <td>{{ $l->deskripsi_loker }}</td>
                    <td>{{ $l->gaji_loker }}</td>
                </tr>
            @endforeach
        </table>
    </body>
</html>

Thank you :)谢谢 :)

in your where query you need to add your table name.在您的 where 查询中,您需要添加表名。 In inner join it joins the two tables.在内部联接中,它联接两个表。 So when you get any column you need to tell which table's column is it.所以当你得到任何列时,你需要告诉它是哪个表的列。 write your function like this.像这样写你的函数。

public function detail($id)
    {
        $usaha = DB::table('perusahaan')
                        ->join('jenis_produk', 'perusahaan.id_jenis_produk', '=', 'jenis_produk.id_jenis_produk')
                        ->join('pengusaha', 'perusahaan.id_pengusaha', '=', 'pengusaha.id_pengusaha')
                        ->where('perusahaan.id_perusahaan', $id)->first();

        $produk = DB::table('produk_usaha')
                        ->join('perusahaan', 'produk_usaha.id_perusahaan', '=', 'perusahaan.id_perusahaan')
                        ->where('perusahaan.id_perusahaan', $id)->get();

        $loker = DB::table('lowongan_kerja')
                        ->join('perusahaan', 'lowongan_kerja.id_perusahaan', '=', 'perusahaan.id_perusahaan')
                        ->where('perusahaan.id_perusahaan', $id)->get();

        return view('detail', compact('usaha', 'produk', 'loker'));
    }

The column id_perusahaan is in the two tables you are joining, so in the WHERE , it doesn't know in which table to apply this condition, so you must specify the table name. id_perusahaan列在您要加入的两个表中,因此在WHERE ,它不知道在哪个表中应用此条件,因此您必须指定表名。

I know that the returned data should have the two columns with the same value and it seems useless to specify the table name, but the SQL doesn't understand that.我知道返回的数据应该有具有相同值的两列,并且指定表名似乎没用,但是 SQL 不理解。

Change ->where('id_perusahaan', $id)->first();更改->where('id_perusahaan', $id)->first(); to ->where('produk_usaha.id_perusahaan', $id)->first();->where('produk_usaha.id_perusahaan', $id)->first(); or ->where('perusahaan.id_perusahaan', $id)->first();->where('perusahaan.id_perusahaan', $id)->first();

暂无
暂无

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

相关问题 Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 where子句中的列&#39;status&#39;不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 where子句中的列&#39;NRP&#39;不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'NRP' in where clause is ambiguous Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 order order中的&#39;created_at&#39;列不明确Laravel 5.5 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 5.5 SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 8 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 8 Yii2 GridView:SQLSTATE[23000]:违反完整性约束:1052 order 子句中的列“id”不明确 - Yii2 GridView: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 完整性约束违规:1052 where子句中的列&#39;id&#39;不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM