which method got this problem ?
Feb 7, 2020
9
Level 11
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');
}
}
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);
}
Please or to participate in this conversation.