#foreach in #foreach have double data laravel - php

I have two array $softwarelist and $virt_software_select when i use #foreach in blade template like below code i got checked but $softwarelist is double in label.
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
#foreach($virt_software_select as $sw_usr)
#if($slst->software==$sw_usr->virtualize_software)
<label>
<input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#else
<label>
<input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#endif
#endforeach
#endforeach
i got these
How can i get checked the same data in two array but not double data with $softwarelist in laravel?

You have to use in_array() php function then it will work and use only one foreach
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
#if(in_array($slst,$virt_software_select))
<?php $checked ="checked"; ?>
#else
<?php $checked =""; ?>
#endif
<label>
<input type="checkbox" {{$checked}} name="virt_software[]" value="{{$slst->software}}"/>
{{$slst->software}}
</label>
#endforeach
Hope it helps!

You have to use in_array() function instead of using nested foreach
$softwarelist =["Adobe Photoshop","Adobe Illustrator","Corel Draw","My SQL","Oracle"];
$virt_software_select=["Adobe Photoshop","My SQL"];
#foreach($softwarelist as $slst)
<label>
#if(in_array($slst,$virt_software_select))
<input type="checkbox" checked name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
#else
<input type="checkbox" name="virt_software[]" value="{{$slst->software}}"/>{{$slst->software}}
#endif
</label>
#endforeach

Related

How do I keep checkbox checked after refresh

0
am trying to check checkbox after i search categories. but its errors
explode(): Argument #2 ($string) must be of type string, array given
<input id="subCats-{{$s->id}}" name="category[]" type="checkbox" value="{{ $s->id }}"
#if (request()->category != "" && in_array($s->id,explode(',', request()->category)))
checked
#endif
>
and am having problem with
<input type="checkbox" name="level" value="0" id="lvl-0"
#if (request()->level == 0)
checked
#endif
it's always is checked because if i don't requests its null and it's thinks that its 0 i tried (request()->level === 0) but it's still don't works.
others with level are working
<input type="checkbox" name="level" value="1" id="lvl-1"
#if (request()->level == 1)
checked
#endif
<input type="checkbox" name="level" value="2" id="lvl-2"
#if (request()->level == 2)
checked
#endif
The problem is your
in_array($s->id,explode(',', request()->category)))
explode generates an array from a string, but your "request()->category might not give you a string, it is already an array.
so maybe its working with this:
in_array($s->id, request()->category ?? []))
I think it's being checked regardless of the if statement. So this might work:
<input type="checkbox" :checked="{{ request()->level === 0 }}" />
To fix the problem with the null / 0, you can add to your condition a check to make sure the value is not null like this:
#if (request()->level == 0 && !is_null(request()->level))
checked
#endif

php Laravel adding inputs with name as an array

I am trying to create a form where the names of the inputs are an array. I am going through an array sent from another view to get fields to show. in this example I want to show 3 fields.
$someResult = array('0','1','2');
$fields = array(0=>'fName',1=>'mName',2=>'lName');
#foreach($someResult as $k){
<td> {!! Form::text($fields[$k][],$someVal) !!}</td>
#endforeach
I tried simplifing it to straight php:
foreach ($someResult as $k){
echo "<tr><td><input type='text' name='".$fields[$k][]."' value='".$someVal."'></tD></tr>";
}
Either way, i get the error "Cannot use [] for reading"
How can i declare name argument as an array?
My goal is something like:
<td>
<input type='text' name='fName[]' value='someVal'>
</td>
<td>
<input type='text' name='mName[]' value='someVal'>
</td>
//etc.....
The square bracket should be used as a string in your dom.
#foreach ($someResult as $k)
<tr>
<td>
<input type="text" name="{{ $fields[$k] }}[]" value="{{ $someVal }}">
</td>
</tr>
#endforeach

I have got two associative arrays of different lengths. I want to know how to compare values and apply to checkboxes?

There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.
Now I have this:
Politics
Video (Checked)
Policy (Checked)
Video
I want
Video (Checked)
Policy (Checked)
My code at the monent:
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
#if($selected->title == $categories->title)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#endif
#endforeach
#endforeach
You can apply that condition inside element to apply checked property
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" {{$selected->title == $categories->title ? 'checked' : ''}}> {{$categories->title}}
#endforeach
#endforeach
I have resolved this problem
Controller:
$checkedCategory = [];
foreach($post->category as $selected) {
for($i = 0; $i < count($category); $i++) {
if($selected->title == $category[$i]->title) {
$checkedCategory[$i] = $category[$i]->title;
}
}
}
$checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));
Template:
<fieldset>
<legend>Категории</legend>
#foreach($category as $categories)
#if(in_array($categories->title, $checkedCategory))
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#else
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#endif
#endforeach
</fieldset>

Printing array to blade

I'm getting an error of
htmlspecialchars() expects parameter 1 to be string, object given.
I'm trying to print an array from session to blade.
view:
<input type="text" name="to" value="{{$mail}}">
controller:
public function view_send_email()
{
$data["_email_list"] = Tbl_press_release_email::get();
$data["sent_email"] = Request::input('sent_email');
$mail = Session::get('email');
return view("send_email", compact('data', 'mail'));
}
You should try this:
#foreach ($mail as $email)
<input type="text" name="to[]" value="{{$email}}">
#endforeach
Note: As you will have multiple values in $email you need to take array of input element as mentioned in above code (i.e name = "to[]")
Updated Answer
#foreach ($mail as $email)
#foreach ($mail as $emails)
<input type="text" name="to[]" value="{{$emails}}">
#endforeach
#endforeach
<input type="text" name="to" value="{{$mail}}">
To
<input type="text" name="to" value="{{print_r($mail)}}">
It seems like it's returning multiple values, so you have to loop through them to display all of them, use a foreach loop.
#foreach ($mail as $email)
<input type="text" name="to" value="{{$email}}">
#endforeach
If you want Form Model Binding
That's a different thing but the same concept, you can view the docs here.
EDIT: It looks like you want to store an array into an input, to do this you must add a [] at the end of the name of your input like this
<input type="text" name="to[]" value="{{$mail}}">
Then when they submit you simply go Input::get('to')[0] to display the first input.

Laravel form checkbox checked flag not working correctly

I have a piece of template:
#foreach ( Permission::all() as $permission )
{{ Form::checkbox('permissions[]', $permission->id, $role->permissions->contains($permission->id) ) }} <label>{{ $permission->label }}</label>
{{ var_dump($role->permissions->contains($permission->id)) }}
#endforeach
this outputs the following html:
<input checked="checked" name="permissions[]" type="checkbox" value="1"> <label>View entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="3"> <label>Edit Entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="4"> <label>Delete entity</label>
<small>boolean</small> false
W T H is going on. anyone any ideas?
EDIT
even moreconfusion. If I pass in false to the Form::checkbox function. Only the first item is rendered without the checked parameter. I give up :(
Found the 'problem'. See this issue on github: https://github.com/laravel/framework/issues/5078 :
my model being parsed has the permissions() array. The false will be ignored and the checkbox will be shown checked. Even if the $permission->id is not related to the Role.
FormBuilder#getCheckboxCheckedState
return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
this line will return true, even if the $permission->id is set. Or the 'checked state' parameter is given.
Perosnally I think that all those isChecked checks should only be performed if $checked is_null.
FormBuilder#checkable()
$checked = $this->getCheckedState($type, $name, $value, $checked);
should be
if ( is_null($checked) ) $checked = $this->getCheckedState($type, $name, $value, $checked);

Categories