radio checked lowest value in blade - php

The following is my laravel blade code. I want automatically check the radio with lowest value.
{
#foreach($service_stages as $ss)
<input class="" id="money" name="money" type="radio" id="" value="{{$ss->money}}" onclick="generateDate()" checked>{{$ss->money}}
#endforeach
}

First write some code to find the smallest value (e.g. in your controller). Then save it to a variable which you provide to your blade view. Lets assume you have it in the variable $smallestValue then you can try the following:
#foreach($service_stages as $ss)
{{ Form::radio('money', $ss->money, ($ss->money == $smallestValue) ? true : false , ['id' => 'money', 'onclick' => 'generateDate()']) }}
#endforeach
This will set the checked="checked" if $ss->money == $smallestValue.

Related

Setting multiple Tag checkboxes as "Selected" from Array

Struggling with something simple!
I'm new to Laravel 8 and have a number of checkboxes which are dynamically created saving their values to an array. My problem occurs when i want to flag previously active options as "selected".
The problem appears to be with the in_array argument and where i'm creating the array..
#php
$currentTags = $prop->tags->pluck('id');
#endphp
#foreach($tags as $tag)
<div class="custom-control custom-switch">
<input type="checkbox"
#if(in_array($tag->id, $currentTags)) selected #endif
class="custom-control-input"
onclick="$(this).val(this.checked ? {{ $tag->id }} : 0)"
id="tags[{{ $tag->id }}]"
name="tags[{{ $tag->id }}]">
<label class="custom-control-label" for="tags[{{ $tag->id }}]">{{ $tag->name }}</label>
</div>
#endforeach
Error
TypeError
in_array(): Argument #2 ($haystack) must be of type array, Illuminate\Support\Collection
As #El_Vanja commented:
The error is rather clear - it expects an array, but you gave it an object of type Illuminate\Support\Collection. If you check out the documentation of that class, you'll see it has a method called all that retreives collection items as an array.
Also should change selected to checked.
change this
#if(in_array($tag->id, $currentTags)) selected #endif
To
#if(in_array($tag->id, $currentTags->toArray())) checked #endif
or
#if(in_array($tag->id, $currentTags->all())) checked #endif

Is it possible to compare Session::get('key') value and compare it using laravel inline if else statement?

what I'm trying to do is a filter radio button. When a user apply the filter based on a specific year, it goes into the controller to filter data results based on that year and return back to the view with new values. However when the page refreshes, the value filtered by the year is reflected, just that the radio button is unchecked and the default All radio button is checked.
What I did was store the radio button's id in a Session at the controller based on value of which radio button has been clicked. If the id of the radio button matches the id stored in the session, it should checked that radio button.
Index.blade.php
<label>
<input type="radio" name="year" id="tab" value="_all" class="flat-red filter" checked="checked"> All
</label>
#foreach ($years as $year)
<label>
<input type="radio" name="year" id="id{{$year->year}}" value="{{ $year->year }}" class="flat-red filter" checked = {{ Session::get('radio') == "id{{$year->year}}" ? 'checked' : 'false' }}> {{$year->year}}
</label>
#endforeach
<button type="submit" class="btn btn-primary btn-sm pull-right">Apply Filter</button>
StatisticsController.php
session(['radio' => 'id'.request('year')]);
return view('statistics.index', $counts, compact('years'));
Error that I am getting
syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)
Method(s) I have tried,
1. Using JS to see which radio button has been click and store it in the local session.
What am I doing wrong or missed out?
Everything inside of {{ }} is PHP. The {{ }} gets replaced by the Blade Compiler with something like this:
<?php echo e(...); ?>
So basically just use regular PHP syntax between {{ }}:
{{ Session::get('radio') == "id{$year->year}" ? 'checked' : 'false' }}
Having {{ }} inside of {{ }} is going to break the compiling of that echo statement.
A solution could be to read that session variable in your controller and inject it into your template.
Don't put more logic into the template, that's not what a template engine is made for - better decouple such logic. This helps you to keep your code maintainable. A blade template does not need to know anything about a session

Laravel 5.2 Blade - Undefined offset: 5 in my loop for

i'm creating admin of my articles, in my resource EDIT post i have some problems, i would like show all tags of my post that i'm editing, i'm tryng like this:
I created a variable array with all tags id relation with the post edited:
$post_tags[0]->tag_id // it return some id of tag's post
Now i would show all tag's post like input checked in my form edit.blade.php
#for ($i = 0; $i < count($all_tags); $i++)
#if($all_tags[$i]->id != $post_tags[$i]->tag_id)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat"/>
#else
<input style="cursor: pointer;" type="checkbox" name="tags[]" id="{{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat" checked/>
#endif
#endfor
But i'm getting this error:
ErrorException in Collection.php line 1187: Undefined offset: 5 (View:
C:\xampp\htdocs\sofis\resources\views\admin\post\edit.blade.php)
Larvael 5.3 and above
As others have said, you need to check to make sure the index exists. You can also simplify your logic some by using #foreach instead of #for:
#foreach ($all_tags as $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$loop->index]) && $post_tags[$loop->index]->tag_id == $tag->id) ? 'checked' : '' }} />
#endforeach
You could improve Samsquanch's answer to match with versions lesser than 5.3. You can access array index by defining $key in foreach() like shown bellow.
#foreach ($all_tags as $key => $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$key]) && $post_tags[$key]->tag_id == $tag->id) ? 'checked' : '' }} />
#endforeach
This means $post_tags[$i] doesn't have element with index = 5. You shouldn't use $array[$index] method if you're not sure element with this index exists. Or you should check it before use:
#if (isset($post_tags[$i]) && $all_tags[$i]->id != $post_tags[$i]->tag_id)
I guess $post_tags does not have index 5, so $post_tags[$i] fails

Looping through input array in Laravel 5

I have a basic UI where users can add a simple list with a label and a value. I want to loop through that list to store the data in a "Detail" model.
I have the following code.
Controller:
$details = $request->input('detail_label');
foreach($details as $key => $value)
{
if(!empty($request->input('detail_value.'.$key))) {
// if the detail has an existing ID
if($request->input('detail_id.'.$key)) {
$detail = Detail::find($request->input('detail_id.'.$key));
} else {
$detail = new Detail;
}
$detail->type = $request->input('detail_type.'.$key);
$detail->label = $request->input('detail_label.'.$key);
$detail->value = $request->input('detail_value.'.$key);
if($request->input('detail_privacy.'.$key) == 1) {
$detail->privacy = 1;
} else {
$detail->privacy = 0;
}
$user->details()->save($detail);
}
}
View:
#foreach($user->details as $detail)
<div class="detail">
<input type="hidden" name="detail_id[]" value="{{ $detail->id }}">
<label>Type
<select name="detail_type[]">
<option #if($detail->type == '1')selected #endif value="1">Phone</option>
<option #if($detail->type == '2')selected #endif value="2">Phone (mobile)</option>
<option #if($detail->type == '3')selected #endif value="3">URL</option>
<option #if($detail->type == '4')selected #endif value="4">Email</option>
</select>
</label>
<label>Label
<input type="text" name="detail_label[]" value="{{ $detail->label }}">
</label>
<label>Value
<input type="text" name="detail_value[]" value="{{ $detail->value }}">
</label>
<label>Private?
<input type="checkbox" name="detail_privacy[]" #if($detail->privacy == true) checked #endif value="1">
</label>
<label>Delete?
<input type="checkbox" name="detail_delete[]" value="{{ $detail->id }}">
</label>
</div><!-- / detail -->
#endforeach
Every aspect of my code works as I had planned except the detail_privacy field. It sets and unsets the boolean privacy attribute but it pays no attention to which $key I want. It always sets it according to the order in the loop. If I just set one detail to be private it will be first. If I set two, (whichever two), it will be the first and second.
Something is clearly wrong with my logic but I can't tell what.
Any help would be really appreciated. Thanks!
The reason it doesn't work is that non-checked checkboxes are not included in the post data. You may see it by dumping value of $request->input('detail_privacy').
To be able both to edit existing and add new details, you need to set keys on inputs in the form. Anything really, as long as you keep track of it. To make things easier you can add hidden input named same as the checkbox, so detail_privacy will be always present in the post data. For example:
#foreach ($user->details as $key => $detail)
<input type="hidden" name="detail_id[{{ $key }}]" value="{{ $detail->id }}">
...
<input type="hidden" name="detail_privacy[{{ $key }}]" value="0">
<input type="checkbox" name="detail_privacy[{{ $key }}]" #if($detail->privacy == true) checked #endif value="1">
...
#endforeach
If you add new fields dynamically ie. with javascript you need to respect those keys. Its quite simple though, just pass last value of $key to your js and you're set.
Also I would recommend different notation for input names: details[{{ $key }}][id] instead of detail_id[{{ $key }}]. This way controller action will become much simpler:
foreach ($details as $detail) {
if (! empty($detail['id'])) {
...
}
}
If the Boolean values are stored as TINYINTS in the Database (as in: 0 or 1); then perhaps taking out the Boolean true || false may resolve the Issue. Not Certain but guessing it's worth the try:
<!-- JUST TRY IT LIKE SO: #if($detail->privacy) checked #endif -->
<label>Private?
<input type="checkbox"
name="detail_privacy[]"
#if($detail->privacy) checked #endif value="1"
>
</label>
Or Hypothetically:
<!-- YOU MAY ALSO USE "1" LIKE SO: #if($detail->privacy==1) checked #endif -->
<label>Private?
<input type="checkbox"
name="detail_privacy[]"
#if($detail->privacy==1) checked #endif value="1"
>
</label>

How to display 2 different values on checking & not checking checkbox in laravel?

The code is like this:
{{ Form::hidden('id') }}
<td class="td-actions">
{{ Form::checkbox('active', 'active',true) }}
{{ Form::hidden('active', 'inactive',false) }}
</td>
{{Form::close() }}
On displaying the value in web browser I get both values-active & inactive in both ckecked & unchecked cases.
How can I get rid of that?
You should have the hidden input before the checkbox, and use numeric values rather than booleans:
{{ Form::hidden('active', '0') }}
{{ Form::checkbox('active', '1') }}
If the checkbox isn’t checked, then you will get a value of 0. If the checkbox is checked, then you will get the checkbox’s value (1).

Categories