simply in my controller i have this collections:
$category = Category::with('users')->find($id);
$users = User::with('roles')->get();
and in front end i try to check which users in $users is into $category->users, like with:
in Category model which that belong to User i have one or multiple stored users and i would like checked html option if each $category has user which that is into $user
<select class="form-control multiselect-filtering" multiple="multiple" name="users[]">
#foreach ($category->users as $guser)
#foreach ($users as $user)
<option value="{{$guser->id}}"
#if ($guser->id == $user->id)
selected="selected"
#else
''
#endif
>
{{$user->username}}
</option>
#endforeach
#endforeach
</select>
this code and compare works, but i have multiple <option> which they are maybe selected or not. how can i solve this issue?
You can loop over the collection which is in the $users variable, and use the contains method to check if the current user id in the loop is contained in the $category->users collection.
#foreach ($users as $user)
<option value="{{$user->id}}"
#if ( $category->users ->contains('id', $user->id))
selected="selected"
#endif
>
{{$user->username}}
</option>
#endforeach
I Want make foreach in behind loping
$aproval = Aproval_position::get();
$user = User::get();
return view('authentication.index_aproval', compact('user','aproval'));
this view blade
#foreach($aproval as $approv)
<label for="" class="col-lg-3">{{ $approv->name_matrix }}</label>
<select>
#foreach($user as $user)
<option value="">{{ $user->name }}</option>
#endforeach
</select>
#endforeach
but when I try to use foreach user inside looping aproval thats error,
I want use User foreach table 2x or more but can't,
can you give me recomended way to use that ?
Change this:
#foreach($user as $user)
<option value="">{{ $user->name }}</option>
#endforeach
To this:
#foreach($user as $item)
<option value="">{{ $item->name }}</option>
#endforeach
I hope that would solve your problem.
I'm using shipping system where will calculate shipping price.
this is what i get in my blade currently:
As you see I get id of state and then name of state.
here is my function:
public function index() {
$data = RajaOngkir::Provinsi()->all();
return view('welcome', compact('data'));
}
and this is my blade code:
#foreach($data as $sss)
#foreach($sss as $numbers)
{{ $numbers }} <br>
#endforeach
#endforeach
here is {{$data}} dump info:
what I want is to getting id and name separately so I can use it
in input.
The foreach will loop through anyhow and pull the relevant info. You just then show it within your blade like so:
#foreach ($data as $info)
ID: {{ $info->province_id }} <br />
Name: {{ $info->province }}
#endforeach
Updated:
From my re read you want it as a form drop down select so you can calculate shipping fee's therefore you'd simply do:
<select name="province" id="">
<option value="">Select</option> -> This will be the default select option
#foreach ($data as $info)
<option value="{{ $info->province_id }}">{{ $info->province }}</option>
#endforeach
</select>
I am trying to fill a <select> dynamically with the chosen option as first option.
Here is my attempt:
<select id= "my_id" name="my_name" size="1" class="my_class">
#foreach($items as $item)
#if($selected_item->item_id == $item->id)
<option selected value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
#foreach($items as $item)
#if($selected_item->item_id != $item->id)
<option value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
</select>
$selected_item contains the ID of the item in other table. $item contains the ID and names of all items.
Any ideas?
Assumptions:
i) $selected_item is an object and doest have property item_id.
ii) $items is an array of objects.
then following code should produce desired results.
<select id= "my_id" name="my_name" size="1" class="my_class">
#foreach($items as $item)
#if($selected_item->item_id == $item->id)
<option selected value="{{$item->id}}">{{$item->name}}</option>
#else
<option value="{{$item->id}}">{{$item->name}}</option>
#endif
#endforeach
</select>
I have an array of tags from my database. I also have attached tags from a current item. I want to check wether tags are already attached or not. This seems to work but of course I get duplicate items in my select-field when there is originally more than 1 tag attached to the item. How can I avoid this ?
<select name="tags" multiple required>
#foreach ($tags as $name)
#foreach($item->tags as $itemtag)
#if($name == $itemtag->name)
<option value="{{$name}}" selected>{{$name}}</option>
#else
<option value="{{$name}}">{{$name}}</option>
#endif
#endforeach
#endforeach
</select>
$tags array :
{"9":"Acoustic","3":"Angry","6":"Autumn","10":"Banjo","13":"Bass","14":"Cheerful","12":"Chill","35":"Dirty","8":"Electric","22":"Epiphone","24":"ESP","19":"Fender","33":"Funk","16":"Funny","18":"Gibson","30":"Gretsch","32":"Groovy","1":"Happy","20":"Ibanez","23":"Jackson","31":"Les Paul","28":"Martin","15":"Mellow","21":"Paul Reed Smith","27":"Redwood","2":"Sad","29":"Schecter","17":"Sleepy","34":"Spanish","7":"Spring","4":"Summer","26":"Taylor","11":"Ukulele","5":"Winter","25":"Yamaha"}
$loop->tags array :
[{"id":1,"created_at":null,"updated_at":null,"name":"Happy","pivot":{"FK_loop_id":2,"FK_tag_id":1}},{"id":4,"created_at":null,"updated_at":null,"name":"Summer","pivot":{"FK_loop_id":2,"FK_tag_id":4}},{"id":14,"created_at":null,"updated_at":null,"name":"Cheerful","pivot":{"FK_loop_id":2,"FK_tag_id":14}},{"id":9,"created_at":null,"updated_at":null,"name":"Acoustic","pivot":{"FK_loop_id":2,"FK_tag_id":9}}]
This should work :
<select name="tags" multiple required>
#foreach ($tags as $name)
#foreach($item->tags as $itemtag)
#if($name == $itemtag->name)
<option value="{{$name}}" selected>{{$name}}</option>
<?php continue 2; ?>
#endif
#endforeach
<option value="{{$name}}">{{$name}}</option>
#endforeach
I might be wrong, your question is still a bit confusing but the following code should do it I think:
<select name="tags" multiple required>
#foreach($item->tags as $itemtag)
#if(in_array($itemtag->name, $tags))
<option value="{{$name}}" selected>{{$name}}</option>
#else
<option value="{{$name}}">{{$name}}</option>
#endif
#endforeach
</select>
Basically all you want to check is that the name property is already in the array of tags so therefore you just need to use in_array to check if the tag array has that value
As i think this is good soluation provide by #Sofiene DJEBALI
I have 1 suggestion for this, using my suggestion you can complete this with only one foreach
-- Update your query and get only name with using pluck() function;
than your query result might be look like i.e $itemtag = array(Acoustic, Cheerful, Happy,Summer);
I am sure it take less time in processing
<select name="tags" multiple required>
#foreach($tags as $key => $name)
<option value="{{$key}}" <?php echo in_array($name, $itemtag)?'selected':'';?>>
{{$name}}
</option>
#endforeach
</select>