Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mehmetanbaki's avatar

Controller

Hey Laravelers

I have this issue

Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select `id`, `name`, `bookings_users`.`booking_id` as `pivot_booking_id`, `bookings_users`.`user_id` as `pivot_user_id`, `bookings_users`.`created_at` as `pivot_created_at`, `bookings_users`.`updated_at` as `pivot_updated_at` from `users` inner join `bookings_users` on `users`.`id` = `bookings_users`.`user_id` where `bookings_users`.`booking_id` in (2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) 

this is the controller

<?php

namespace App\Http\Controllers;

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

class BookingController extends Controller
{

    public function index()
    {
        $bookings = Booking::with(['room.roomType', 'users:id,name'])->paginate(10);
        return view('bookings.index')
            ->with('bookings', $bookings);
    }


    public function create()
    {
        $users = DB::table('users')->get()->pluck('name', 'id')->prepend('none');
        $rooms = DB::table('rooms')->get()->pluck('number', 'id');
        return view('bookings.create')
            ->with('users', $users)
            ->with('booking', (new Booking()))
            ->with('rooms', $rooms);
    }


    public function store(Request $request)
    {
        $booking = Booking::create($request->input());
        $booking->users->attach($request->input('user_id'));

        return redirect()->action('BookingController@index');
    }


    public function show(Booking $booking)
    {
        return view('bookings.show', ['booking' => $booking]);
    }


    public function edit(Booking $booking)
    {
        $users = DB::table('users')->get()->pluck('name', 'id')->prepend('none');
        $rooms = DB::table('rooms')->get()->pluck('number', 'id');
        $bookingsUser = DB::table('bookings_users')->where('booking_id', $booking->id)->first();
        return view('bookings.edit')
            ->with('bookingsUser', $bookingsUser)
            ->with('users', $users)
            ->with('rooms', $rooms)
            ->with('booking', $booking);
    }


    public function update(Request $request, Booking $booking)
    {
        $booking->fill($request->input());
        $booking->save();
        $booking->users()->sync([$request->input('user_id')]);
        return redirect()->action('BookingController@index');
    }


    public function destroy(Booking $booking)
    {
        $booking->users()->detach();
        $booking->delete();
        return redirect()->action('BookingController@index');
    }
}


0 likes
9 replies
Rymercyble's avatar

are you sure about syntax inside ->with() method ? there should be names of relation definitions inside your model

shushkin's avatar

It's looks like you need add table name to

select `id`

from users table or bookings_users

select `users.id`
or 
select `bookings_users.id`
fylzero's avatar
fylzero
Best Answer
Level 67

@mehmetanbaki You're using a join so your ids and any other field that might be that same must be selected explicitly.

Instead of posting the entire controller and not posting the route... can you just post the method that is hitting this problem?

Try this... untested.

public function index()
    {
        $bookings = Booking::with(['room.roomType', 'users:users.id,name'])->paginate(10);
        return view('bookings.index')
            ->with('bookings', $bookings);
    }

If that doesn't work, might as well just pull the entire users table. It won't add a ton of strain to do so.

public function index()
    {
        $bookings = Booking::with(['room.roomType', 'users'])->paginate(10);
        return view('bookings.index')
            ->with('bookings', $bookings);
    }
mehmetanbaki's avatar

@fylzero

As you can see I'm using it just when I changed the store, update and destroy functions it's gave me that error

fylzero's avatar

@mehmetanbaki Because you're doing a join with another table... so it can't distinguish between users.id and bookings_users.id (or whatever tables you're joining)... because it is seeing both of those as simply id.

Please or to participate in this conversation.