How to Show Checkboxes as Checked When Values Are Set in the Database in Laravel

Laravel set checkboxes checked based on database value

This is how I resolved it, I added foreach and if statement in the input to check if it the same value as from database:

             <div class="form-group" id="ageCheckboxes">
@foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" value="{{$age->id}}" name="age_group[]" id="age_group{{$age->id}}" @foreach ($nanny_babysitting_ages as $ages) @if($age->id == $ages->ages ) checked @endif @endforeach />
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
@endforeach
</div>

How to check only stored checkbox value in database in laravel?

The problem is that your $category will always be set this way, it will always return true.

You are probably iterating over all the categories, so you need to compare the category with for example the product or whatever you have in the view, and check if the category id is the one selected for that product.. something like this:

@foreach($categories as $category)
<div class="col-lg-3">
<div class="form-group">
<input type="checkbox" name="category[]"
value="{{$category->id}}"
@if($category->id === $model->category_id) 'checked' @endif>
{{ $category->name }}<br>
</div>
</div>
@endforeach

Now this code will check the category only if it was attached to the model that you are listing. Show more code from the controller maybe and the view to get better help if this is not sufficient for you.

-- EDIT

Based on your edit, you are using a many to many relationship there.. so this will do it:

@if($place->categories->contains($category->id)) 'checked' @endif

Show checkbox checked from database in laravel

Your code will not work because you are trying to compare array with a string, which is impossible. you can use php in_array function to check whether your permission exist for the current permission or not

I think you are trying to check all the permission which already exist for the specific roles. correct me if i am wrong.

Try this

<input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
@if($role->permissions) @if(in_array($permission->id, $role->permissions->pluck('id')) checked @endif @endif>

Hope this will help :)

How to get selected checkbox value in edit blade Laravel

This is not a laravel question. But you can use conditional after value attribute inside input tag.

Try this code:

<input type="checkbox" value="1" {{$ticket->$id == 'student_id' ? 'checked' : ''}}/>

How to show checkboxes as checked when values are set in the database in Laravel

First you make an array with all data

<?php 
$all_data = array();
foreach($roles as $role){
$all_data[] = $role->id;
}
?>

Now you create checkbox

@foreach ($roles as $role)
{{ Form::checkbox('role[]', $role->id, in_array($role->id, $all_data)) }}
{{ Form::label('role', $role->name) }}<br>
@endforeach

How to check a checkbox based on DB query result in Laravel?

Get the column which you need to compare as below

$brng_selected = DB::table('Cprefs')
->select('qatype') // column name which you need
->where('user_id', $u_id)
->get()->toArray(); // it will return as array

return view('prefCat', ['b_sel' => $brng_selected]);

In your view change as below :

<input type="checkbox" name="catpref[]" value=11 {{ (in_array(11,$brng_selected)?"checked":'') }} > Sports<br>


Related Topics



Leave a reply



Submit