Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 623
Niladri Banerjee - Uttarpara Avatar asked Jun 19 '18 06:06

Niladri Banerjee - Uttarpara


People also ask

How can check checkbox is checked or not in laravel blade?

“check if checkbox checked laravel blade” Code Answer //If the checkbox is checked, then the $request->check will not be null. Otherwise, it will. //Then the $isChecked variable will be true if the checkbox is checked, and false if it isn't.

How to work with checkbox input in a Laravel form?

This blog post shows various examples of how to work with checkbox input in a Laravel Form. Examples include validating a single checkbox, checking in the code if the checkbox is clicked, dealing with Multiple checkboxes, etc. If you just have a single checkbox in your Laravel form.

Is it possible to do all this in Laravel blade?

but you can’t do all that in blade. Please do read the documentation and watch youtube videos. A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

How to load checkboxes from the database?

So in order to load your checkboxes from the database or to test if a checkbox is checked : First of all you need to understand how it works, as @itachi mentioned : { { Form::checkbox ( 1st argument, 2nd argument, 3rd argument, 4th argument ) }} Fourth argument : additional attributes (e.g., checkbox css classe)

What is Laravel?

Blade Templates - Laravel - The PHP Framework For Web Artisans Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things. Skip to content Prologue Release Notes Upgrade Guide


1 Answers

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
like image 187
athulpraj Avatar answered Oct 13 '22 21:10

athulpraj