Laravel - Form checkbox not getting checked

597 Views Asked by At

In Laravel I am fetching the value from database and based on that adding condition to check checkbox. I am facing issue with laravel checkbox is not getting checked. Tried using following code:

// outputting value from database to input field
{{Form::text('lock_user_role',null,array('class'=>'form-control')) }} 
              
// if lock user value is 1 in database then need to check checkbox 
{{Form::checkbox('lock_user_role', 'lock_user_role', 'lock_user_role' === '1' ? true : false)}}

Here is my database column structure: enter image description here

Here is my column value getting stored:

enter image description here

Here is my actual output shown:

enter image description here

As shown in above image, value is showing 1 but checkbox is not getting checked. Can anyone correct my code ? Thanks

1

There are 1 best solutions below

3
Donkarnash On

If the value fetched from database is integer 1 then the strict comparison (===) will evaluate to false.

Try replacing with loose comparison (==)

And comparison between string literal lock_user_role and any other string or integer will always be false 'lock_user_role' == '1' will always evaluate to false - as pointed out by @TimLewis.

So it should be something like $user->lock_user_role or $lock_user_role - a variable

// if lock user value is 1 in database then need to check checkbox 
{{Form::checkbox('lock_user_role', 'lock_user_role', $user->lock_user_role == '1' ? true : false)}}