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.
Related
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 to update two values in my data I want to update the first value as an int and the other one is string.
TABLE STRUCTURE
here is my input, the value aircraft_id is the first one to update and the other one is the aircraft_refistration_number
<select name="aircraft_id" class="form-control" id="">
<option value="0" disabled="true" selected="true"> Select </option>
#foreach ($aircrafts as $aircraft)
<option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>
#endforeach
</select>
here is my table
here is where i update the registered_company_name which has to be string but then the output is the aircraft_id
$txtDescript = $request->input('aircraft_id');
$aircraft = DB::table('settings')
->where('id', 4)
->update(['description' => $txtDescript]);
here is the aircraft_id which should be int or id's
$airid = $request->input('aircraft_id');
$aircraft = DB::table('series')
->update(['aircraft_id' => $airid]);
this one perfectly works
here is my output of the problem
WHEREAS it should be like THIS output which should be correct
you can take this longer form,
<select name="aircraft_id" class="form-control" id="">
<option value="0" disabled="true" selected="true" id="airselect"> Select </option>
#foreach ($aircrafts as $aircraft)
<option value="{{ $aircraft->aircraft_id }}">{{ $aircraft->aircraft_registration_number }}</option>
#endforeach
</select>
<input type="hidden" name="aircraft_name" id="aircraft_name" value="">
and set the value of the hidden field using jquery like so
$(document).ready(function(){
$(document).on('change','.airselect',function(){
$selected=$( "#airselect option:selected" ).text();
$('#aircraft_name').val($selected);
});
});
and then adjust your controller like so
$txtDescript = $request->input('aircraft_name');
$aircraft = DB::table('settings')
->where('id', 4)
->update(['
description' => $txtDescript]);
and
$airid = $request->input('aircraft_id');
$aircraft = DB::table('series')
->update(['aircraft_id' => $airid]);
try it and let me here from you, i did not test the code
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 )) }}
So far I know how to populate dropdown using Eloquent (and pluck) is this
public function create()
{
$items = Item::all()->pluck('name', 'id');
return view('admin.item.create', compact('items'));
}
And with this
{!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
I can generate this
<select name="items" class="form-control">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
But now I want to create something like this
<select name="items" class="form-control">
<option value="1">Item 1 - Location</option>
<option value="2">Item 2 - Location</option>
<option value="3">Item 3 - Location</option>
</select>
I can only use pluck with 2 attributes. But now I want it to display 3 attributes. How do I do that? I'm using Laravel 5.3 by the way. Thanks for the help.
You have an array like [1=> "something",2=>"else"]. So you task is to either iterate the array and edit the values or select the the locations from the database and the use foreach or array_filter
So something like the following:
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location->name; // build string to display here
}
Edit:
Your comment said location is at the same table so it would look like...
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location; // build string to display here
}
Pluck location also
public function create()
{
$items = Item::all()->pluck('name', 'id', 'location');
return view('admin.item.create', compact('items'));
}
Generate like this
<select name="items" class="form-control">
#foreach($items as $item)
<option value="{{ $item->id }}">{{ $item->name }} - {{ $item->location }}</option>
#endforeach
</select>
Best way to create list of array is.
public function create(){
$items = Item::where('id','<>','')->lists('name', 'id');
return view('admin.item.create', compact('items'));}