Getting selected checkbox from database - laravel - laravel

I am able to save checkbox values into my database. When i am fetching, i try to get the selected checkbox as below but nothing is selected.. What could i be doing wrong in my code please ?
View
<ul class="list-unstyled mb-0">
#foreach($permission as $value)
<li>
<label class="fancy-checkbox mb-0">
<input type="checkbox" in_array($value->id, $rolePermissions) ? true : false value="{{$value->id}}" name="permission[]">
<span>{{ $value->display_name }}</span>
-
<span>{{ $value->description }}</span>
</label>
<hr>
</li>
#endforeach
</ul>

You're missing the .blade syntax, and you should use 'checked' instead of true : false:
<input type="checkbox" value="{{ $value->id }}" name="permission[]" {{ in_array($value->id, $rolePermissions) ? 'checked' : '' }}/>
For checkboxes, simply setting checked is all that is required to determine if it should be initially checked or not.

Related

The update does not return the selected checkbox

I'm having an error updating the checkbox when I return to the view containing it; in particular it always returns me the first checked checkbox but not the values selected before the update.
Can anyone help me to solve this problem?
<div class="mb-3">
<div for="treatment" class="form-label">All treatments</div>
#foreach ($treatments as $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" {{ $treatment->id == 1 ? 'checked' : null }}>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
</div>
{{ $treatment->id == 1 ? 'checked' : null }}
In this code, you only added 'checked' attribute if the $treatment->id is 1, so only the first checkbox will be checked.
#foreach ($treatments as $key => $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" {{ $treatment->id == $key ? 'checked' : null }}>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
I solved the following:
<div class="mb-3">
<div for="treatment" class="form-label">All treatments</div>
#foreach ($treatments as $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" #if (count($quote->treatments->where('id', $treatment->id))) checked #endif>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
</div>

Show an attribute from other model in blade (Laravel)

I´ve got this exception in my blade template. I made a relation between my two models (RegisteredCourses y User) and I can see it works in the rest of Blade´s template, except form.blade.php
Trying to get property 'user' of non-object (View: C:\laragon\www\hr-english\resources\views\registeredCourse\form.blade.php) (View: C:\laragon\www\hr-english\resources\views\registeredCourse\form.blade.php)
My idea is to show in my blade template the name of the user, but I need in the value of input the correspondant user_id.
I don´t what is the correct approach to this problem.
<div class="form-group {{ $errors->has('course_id') ? 'has-error' : '' }}">
<label for="course_id" class="col-md-2 control-label">Course</label>
<div class="col-md-10">
<select class="form-control" id="course_id" name="course_id">
<option value="" style="display: none;" {{ old('course_id', optional($registeredCourse)->course_id ?: '') == '' ? 'selected' : '' }} disabled selected>Select course</option>
#foreach ($courses as $key => $course)
<option value="{{ $key }}" {{ old('course_id', optional($registeredCourse)->course_id) == $key ? 'selected' : '' }}>
{{ $course }}
</option>
#endforeach
</select>
{!! $errors->first('course_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : '' }}">
<label for="status_course" class="col-md-2 control-label">Name</label>
<div class="col-md-10">
<input class="form-control" name="user_id" type="text" id="user_id" value="{{ old('user_id', optional($registeredCourse->user->name)) }}" minlength="1" placeholder="Enter name here..."> <!--Problwm here-->
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('status_course') ? 'has-error' : '' }}">
<label for="status_course" class="col-md-2 control-label">Status Course</label>
<div class="col-md-10">
<input class="form-control" name="status_course" type="text" id="status_course" value="{{ old('status_course', optional($registeredCourse)->status_course) }}" minlength="1" placeholder="Enter status course here...">
{!! $errors->first('status_course', '<p class="help-block">:message</p>') !!}
</div>
</div>
Use the null coalescing operator: $registeredCourse->user->name ?? null instead of optional($registeredCourse->user->name) in your blade
UPS: Here's a demo showing how this works depending on whether $registeredCourse->user is set or not.

Laravel Checkbox not getting set correctly

Given the following code, all my checkboxes (roles) are marked as checked, even though the user only has one role.
Using Laravel and Spatie Laravel Permissions Package.
I tried the same code in Tinker and it comes back with True, False, False, so it should be working...
#foreach ($roles as $role)
<div>
<label>
<input type="checkbox" value="{{ $role->name }}" checked="{{ $user->hasRole($role->name) ? 'checked' : '' }}">
<span>
{{ $role->name }}
</span>
</label>
</div>
#endforeach
Change your input element to:
<input type="checkbox" value="{{ $role->name }}" {{ $user->hasRole($role->name) ? 'checked' : '' }}>
Checkboxes have a simple checked attribute, you don't assign a value to it.
W3 Example

Multiple Checkbox Saving to DB 1 or 0

Creating multiple question checkboxes and I need to send two possible cases 1 or 0 to the database. However, it always sends the last one I click as 1 all the others 0. So for example, if a user clicks/checks the first one and the last I want to send to the database both 1 and the other two unchecked 0.
Controller
<?php
public function create($request)
{
foreach ($request['options'] as $key => $option) {
Answer::create([
'question_id' => $this->get(),
'text' => $option,
'correct' => $request['correct'] == $key ? 1 : 0
]);
}
}
View
#for($i = 1; $i<=4; $i++)
<div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}" id="option{{ $i }}">
<div class="checkbox col-xs-2 control-label" style="margin-top: -2px">
<label>
<input id="cc" type="checkbox" name="correct" value="{{$i}}" {{ $i==1 ? 'checked' : '' }} >
<!--
{!! Form::hidden('correct',0) !!}
{!! Form::checkbox('correct',1,false) !!}
-->
</label>
</div>
<div class="col-xs-8">
<input type="text" name="options[{{ $i }}]" value="{{ old('options.'.$i) }}"
class="form-control" placeholder="#lang('general.option') {{ $i }}">
#if($errors->has('options.'.$i))
<div class="col-xs-12"></div>
<span class="help-block">
<strong>{{ $errors->first('options.'.$i) }}</strong>
</span>
#endif
</div>
</div>
#endfor
Either mention array of control for checkbox as below
<input id="cc" type="checkbox" name="correct[]" value="{{$i}}" {{ $i==1 ? 'checked' : '' }} >
change name="correct" to name="correct[]"
OR
give different name to each checkbox using incremental variable like
name="correct" to name="correct_".$i

Showing Nested Data in Laravel Blade

I have a nested data like this which is passed to blade-
I want to display it's data in blade view.
So what I have done is-
<ul class="dropdown-menu h-ctr h-ctr2 col-md-12 col-sm-12">
#foreach ($categories as $category)
<li class="no-border">
<label class="pull-left">
<input type="checkbox" value="{{ $category->id }}" checked>
<strong> {{ $category->name }} (21)</strong>
</label>
<ul>
#foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
And I am getting error for nested loop's part-
foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
#endforeach
The error is like this-
Can anyone please help?
Thanks in advance for helping.
Try this mate
#foreach($category['sub_category'] as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
#endforeach
in case this didnt work can you share you controller code too?
EDIT : in your controller try to convert the array to a collection (there are simpler why like using eloquent
$collection = collect($myarray);
Try this..
#if($category->sub_category)
#foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
#endforeach
#endif

Resources