Checkboxes in Laravel

Viewed 28

I'm learing laravel and have some checkboxes on my form. However by default they have the value of on and off -> null or not passed.

But in my tables are using a boolean of 0 and 1. I've covered for this in my controller which takes the on or off values and puts them to 0 or 1 .

        HotelFacility::create([
            'hotel_id' => $hotel->id,
            'fitness_centre' => $request->has('fitness_centre') ? 1 : 0,
            'bar' => $request->has('bar') ? 1 : 0,
        ]);

But how do I now cover for this in my blade template as it's now reading back the opposite?

  <input type="checkbox" name="fitness_centre" value="{{ $hotel[0]->hotelFacilites->fitness_centre ? 'on' : 'off' }}">
    Fitness Centre
  </label>
1 Answers

Please try this because if you want to check the checkbox as per database value so you need to put checked not value.

<input type="checkbox" name="fitness_centre" {{ $hotel[0]->hotelFacilites->fitness_centre ? ' checked ':''}}>
Related