Home > Back-end >  How to validate in Laravel id_room is not available when books table has 1 id room at same day
How to validate in Laravel id_room is not available when books table has 1 id room at same day

Time:01-04

I'm stuck in validating id_room when books table has 1 id room at same day.

i use this because the hotel has 2 recepsionist

        $request->validate([
            'id_room' => ['required', 'integer', Rule::unique('books')->where(function ($query) use ($request) {
                return
                    $startDate = date('Y-m-d');
                $hotelId =
                    Auth::user()->id_hotel;
                $query->where('id_room', $request->id_room)->whereDate('book_date', $startDate)->where('id_hotel', $hotelId);
            })],
            'guestname' => ['required', 'string', 'max:255'],
            'nik' => ['required', 'string', 'max:255'],
            'nota' => ['required', 'string', 'max:255'],
            'price' => ['required', 'integer'],
        ]);

CodePudding user response:

I think your query is fine. It's just the return is $startDate.

Try to return the query instead and change how you use whereDate

'id_room' => ['required', 'integer', Rule::unique('books')
      ->where(function ($query) use ($request) {
      return $query
        ->where('id_room', $request->id_room)
        ->whereDate('book_date', '=', date('Y-m-d'))
        ->where('id_hotel', auth()->user()->id_hotel);
       })
],
  • Related