'Laravel Blade checkbox not checked

I'm passing variables into the blade from controller. This is my code from controller:

$offerConditionsData = Offercondition::where('offerId', $id)->where('deleted', '0')->get();
return view('Administrator.offers.add', $data);

I'm defining a variable at the top of the blade file:

@if(isset($offerConditionsData))
@foreach ($offerConditionsData as $offerConditions)
@php $selectedCondCheckbox = $offerConditions->keyword;
@endphp
@endforeach
@endif

If I echo the $selectedCondCheckbox, then I found the below:

customer_get_discounts_and_special_offers_on_certain_product_or_categories

Now I want that if the variable value matched with a constant, the correct checkbox will checked. Below are the code in the blade file to check the checkbox from a bunch of check boxes.

<input type="radio" name="condition_tab" value="1" class="flat-red" style="position: absolute; opacity: 0;" @php if(isset($selectedCondCheckbox) == 'customer_get_discounts_and_special_offers_on_certain_product_or_categories') { echo "checked=checked"; } else { echo ""; } @endphp>

<input type="radio" name="condition_tab" value="2" class="flat-red" style="position: absolute; opacity: 0;" @php if(isset($selectedCondCheckbox) == 'discount_on_total_amount_of_shipping') { echo "checked=checked"; } else { echo ""; } @endphp>

However, I saw that second checkbox is checked. It should checked the first checkbox as the value of the php variable ,matched with the constant.



Solution 1:[1]

There is certainly some mistake with your if condition

try the conditions separately

       @php if(isset($selectedCondCheckbox)) { 
            if($selectedCondCheckbox =='discount_on_total_amount_of_shipping') 
            { echo "checked=checked"; }}@endphp

Solution 2:[2]

try it with this Code

<input type="radio" name="condition_tab" value="1" class="flat-red" style="position: absolute; opacity: 0;" {{ isset($selectedCondCheckbox) ? $selectedCondCheckbox == 'customer_get_discounts_and_special_offers_on_certain_product_or_categories' ? 'checked=checked' :'' :'' }} >

<input type="radio" name="condition_tab" value="2" class="flat-red" style="position: absolute; opacity: 0;" {{ isset($selectedCondCheckbox) ? $selectedCondCheckbox == 'discount_on_total_amount_of_shipping' ? 'checked=checked' :'' :'' }}>

Solution 3:[3]

Recently laravel added @checked function

<input type="checkbox"
        name="active"
        value="active"
        @checked(old('active', $user->active)) />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 athulpraj
Solution 2 Malkhazi Dartsmelidze
Solution 3 Akbarali