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
Related
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 pass two array from controller to view and then i want option to select when the value is the same. But i want to show all data from $arr_1 in select. and my result i get duplicate data in my select.
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
#foreach($arr_1 as $val)
#foreach($arr_2 as $value)
#if($val==$value)
<option selected>{{$val}}</option>
#else
<option>{{$val}}</option>
#endif
#endforeach
#endforeach
Any solution for these?
It can be done without two foreach
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
<select multiple>
#foreach($arr_1 as $val)
#if(in_array($val,$arr_2))
<option val="{{$val}}" selected>{{$val}}</option>
#else
<option val="{{$val}}" >{{$val}}</option>
#endif
#endforeach
</select>
Demo
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>
Trying to set the Logged in user as a drop down selected value but when i check the list of drop down value it shows me repeated values like Admin.I am generating drop down values from users table stored in database....Please Help to resolve this issue.
Here is the DropDown of Users and Admin is selected as current user.
Users Controller:
$users = User::all();
$current_user = Auth::user()->name;
Dynamic Drop Down in Laravel Blade..
<select name="recieved_by" class="form-control">
<option value="{{ $current_user }}" selected>{{ $current_user }}</option>
#foreach($users as $user)
<option value="{{$user->name}}">{{ $user->name }}</option>
#endforeach
</select>
<select name="recieved_by" class="form-control">
#foreach($users as $user)
#if($current_user==$user->name)
<option value="{{ $current_user }}" selected>{{ $current_user }}</option>
#endif
<option value="{{$user->name}}">{{ $user->name }}</option>
#endforeach
</select>
Use groupBy in your query to remove duplicates.
Also remove current user from the query so that it will not repeat in your select drop down like below:
$current_user = Auth::user()->name;
$users = User::where('name','!=',$current_user)->groupBy('name')->get();
$users = array(); //declare an array
#foreach($users as $user)
$users['name'] = $user->name;
#endforeach
$users = array_unique($users);
before you use your foreach() loop
Hope this will help you.
Please check the syntax as am not a laravel developer
Thanks,
I want to fetch drop down option value & name from database, to do that my controller code is
$roles = Role::pluck('role','user_id');
return view('users.add_role',compact('roles'));
to fetch this data from drop down list my view file code is
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role->user_id }} "> {{ $role->role }} </option>
#endforeach
</select>
but it says error
Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)
if i just only use
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role }} "> {{ $role }} </option>
#endforeach
</select>
then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?
Your $roles variable containt an array which is looks like
[0] => 'your_role'
[1] => 'your_role'
Where is 0 and 1 index is user_id. So your user_id is set as an index of your $roles array. Simply do it dd($roles) and check the output. $key as $role will work. Where $key will containt the user_id.
#foreach($roles as $key => $role)
<option value="{{ $key }} "> {{ $role }} </option>
#endforeach
I use laravel collective package to generate dropdown list much more easier,
you can do things like, on your blade template and it will render the list for you
{{ Form::select('role', $role )) }}