0

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

2
  • Look at this line: lock_user_role' === '1' ? true : false. That is never going to be true. When would the string 'lock_user_role' ever strictly equal '1'? Never. You're basically calling Form::checkbox('lock_user_role', 'lock_user_role', false) so it should be obvious why it's not being checked. Commented Jun 16, 2022 at 18:35
  • Hint, did you maybe mean to use the old helper, or a reference to a Model instance, like old('lock_user_role', $user->lock_role_user) == 1 ? true : false Commented Jun 16, 2022 at 18:36

1 Answer 1

1

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)}}
Sign up to request clarification or add additional context in comments.

3 Comments

Valid point on loose comparison, but 'lock_user_role' == '1' is still false 🙂
@TimLewis Haha yeah. Completely missed that a string literal is being compared to another string. lock_user_role should be a variable like $user->lock_user_role or $lock_user_role
No worries! I thought the same thing as you at first. That looks better, as long as $user (or whatever) is defined in the form. Cheers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.