Dynamic Dropdown showing duplicate value Laravel 5.2 - php

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,

Related

laravel avoid multiple Select's Option in blade

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

Can I Use 2 foreach from variable database in laravel?

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.

laravel 5.4 form dropdown from database

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 )) }}

Count total entries and make a number list in Laravel

I'm trying to make a sort for a project I'm doing, and I want to get the total of entries of my database and then make a select with the total of entries but ordered by 1 to 25 let's say..
My controller looks like this:
$users = User::orderBY('sort', 'DESC')->paginate(10);
return view('admin.user.index', compact('users'));
My view looks like this:
<select class="form-control" id="sort" name="sort">
#foreach($users as $user)
<option value="0">0</option>
#endforeach
</select>
Let's say I have 25 users. I want 25 options from 1 to 25 in the value, since I can't use the item id.. because they can be deleted.
You can try as:
<select class="form-control" id="sort" name="sort">
#foreach($users as $key => $user)
<option value="{{ $key + 1 }}">
{{ $key + 1 }}
</option>
#endforeach
</select>
So you want to set the option's values from 1 to the total number of users? You can just simply update your foreach block.
On your controller, fetch the user records:
$users = User::orderBy('sort', 'desc')->get();
return view('admin.user.index', compact('users'));
And update you view:
<select class="form-control" id="sort" name="sort">
#foreach($users as $index => $user)
<option value="{{ $index + 1 }}">{{ $index + 1 }}</option>
#endforeach
</select>
This way you can display an option with the value from 1 to the total number of users.
Note, if you don't really use any of user's properties on you view, you can just fetch the total number of users.
On your controller, fetch total number of users:
$totalUsers = App\Models\User::orderBy('id', 'desc')->count();
return view('test', compact('totalUsers'));
Then update your view like so:
<select class="form-control" id="sort" name="sort">
#for ($i = 1; $i <= $totalUsers; $i++)
<option value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
Hope this answer you question.

Laravel render dropdown list from related table under employee table

I have 2 tables. Employee table and Roles tables. In my employee.show blade I want to render the dropdown list of the role already assgined or available to assign. I am not using collective forms and am a little stuck as all tutorials use thse.
My controller
public function show($employee)
{
$employee = Employees::find($employee);
$roles = Roles::pluck('role_name', 'id');
return view('employees.show')->withEmployee($employee)->withRoles($roles);
}
and my show.blade. Question is how do i pull the complete list from db of all other roles so a user can update?
<div class="form-group">
<label class="col-md-4 control-label" for="roles_id">Role</label>
<div class="col-md-4">
<select id="roles_id" name="roles_id" class="form-control">
<option value="{{ $employee->roles->id }}">{{ $employee->roles->role_name }}</option>
</select>
</div>
</div>
What you've done in your controller seems correct. Your view however should loop through the roles and create options for them. Bear in mind, that you've used Role::pluck('role_name', 'id'), which will return an array of 'id' => 'role_name'.
You can decide which one is selected by adding a selected attribute to the employee's role option by using a ternary statement, which compare's the employee's role_id to the role_id we're currently looking at in the loop.
Try something like this:
<select name="roles_id">
<option value="">Select...</option>
#foreach ($roles as $roleId => $roleName)
<option value="{{ $roleId }}"{!! $employee->role_id == $roleId ? ' selected' : '' !!}>{{ $roleName }}</option>
#endforeach
</select>

Categories