简体   繁体   中英

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 :

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)

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)

<!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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM