Necesito contar la cantidad de registros del campo accommodation_id en la tabla bookings pero me muestra el siguiente error:
Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bookings.accommodation_id' in 'field list' (SQL: select count(bookings.accommodation_id) as count from
accommodations
whereaccommodations
.user_id
is null andstatus
in (PUBLISHED, DRAFT, PAUSED) group bybookings
.id
)
public function index()
{
$id = Auth::id();
$properties = Accommodation::with([ 'user' => function ($query) { $query->with('bookings'); } ])
->where('accommodations.user_id', $id)
->whereIn('status', ['PUBLISHED', 'DRAFT', 'PAUSED'])
->selectRaw('count(bookings.accommodation_id) as count')
->groupBy('bookings.id')
->get();
return view('admin/accommodations/index', compact('properties'));
}
Modelos:
class Accommodation extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function bookings()
{
return $this->hasMany(Booking::class);
}
}
class Booking extends Model
{
public function accommodation()
{
return $this->hasOne(Accommodation::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}