1

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>
ottz0
  • 2,595
  • 7
  • 25
  • 45
  • 2
    Checkboxes don't send their value to the server if they are not checked. Rather than "value" which would send if checked, you're probably looking to add the "checked" html attribute conditionally: `hotelFacilites->fitness_centre ? ' checked ':''}}>` – Kevin Y Jun 10 '22 at 05:14
  • If for some reason you need to also be able to send a value to the server when the checkbox is not checked, you can use this technique: https://stackoverflow.com/a/1992745/17599576 – Kevin Y Jun 10 '22 at 05:19

1 Answers1

0

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 ':''}}>
VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34