drop-down show selected value when editing a row laravel - html

<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control">
#if ($destinationsData != [])
#foreach($destinationsData as $info)
<option value="{{$destinationsData[$index]}}">{{$info}}</option>
#endforeach
#endif
</select>
</div>
</div>
I retrieve the $index of the selected $destinationData and when dd($destinationsData[$index]); I get the result that I need but it doesn't appear when I put it in the value as shown above

When constructing your <select> and <options>, you need to set a value for each that can be referenced when editing your record. See the following:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control>
#foreach($destinationsData as $key => $info)
<option value="{{ $key }}">{{ $info }}</option>
#endforeach
</select>
</div>
</div>
Setting value="{{ $key }}" will, in this instance, set the value to a 0-based index, which can be referenced when editing:
#foreach($destinationsData as $key => $info)
<option value="{{ $key }}" {{ $record->from_destination_data == $key ? 'selected' : '' }}>{{ $info }}</option>
#endforeach
As long as $record->from_destination_data equals any of the $key iterations, that <option> will have selected="selected", setting it as the default selected option.

Related

How to get old values of multiple select2 dropdown on form in laravel 8?

I am using multiselect dropdown but not able to get old values(by using old('') in laravel blade)on submit.Please tell where i am doing wrong.Below i am sharing code.
<div class="form-group">
<label for="integration">Integration</label>
<div class="select2-purple">
<select id="integration" class="form-control form-select form-select-lg col-lg-12 select2" multiple="multiple" aria-label=".form-select-lg example" data-dropdown-css-class="select2-purple" name="integration[]">
<option value=''>Select Integration</option>
#foreach($integartion as $integartions)
<option value="{{ $integartions->ecom_channel }}" #if(isset($company) && in_array($integartions->ecom_channel, $CmpIntegartion) ) selected #endif>{{ $integartions->ecom_channel }}</option>
#endforeach
</select>
#if ($errors->has('integration'))
<span class="text-danger">{{ $errors->first('integration') }}</span>
#endif
</div>
</div>
<select name="integration[]">
<option value=''>Select Integration</option>
#foreach($integartion as $integartions)
<option value="{{ $integartions->ecom_channel }}" #if(old('integration') && is_array(old('integration')) && in_array($integartions->ecom_channel, old('integration')) selected #endif>{{ $integartions-> ecom_channel}}</option>
#endforeach
</select >
Your data will be stored like this in old:
integration[0] = somevalue,
integration[1] = somevalue
So you first check if your old('integration') is not null,
then you check if its an array, then you check if the current item of loop exists inside that array!!

laravel retrive old() wherein query values

I have 2 multiple value select lists that I pass users between them using jquery.
<select multiple="multiple" id="ListAllUsers" class="form-control">
#foreach ($users as $user)
<option class="content" value="{{ $user->id }}">{{ $user->name }}</option>
#endforeach
</select>
<select name="user_id[]" required multiple="multiple" id="SelectedUsers" class="form-control">
#if (!empty(old('user_id')) && $create_form)
#foreach ($old_value_users as $old_user)
<option class="content" value="{{ $old_user->id }}">{{ $old_user->name }}</option>
#endforeach
#endif
</select>
Now, my issue is that when I go and pass the old('user_id') value in the whereIn query, I only get the first result of the array instead of all of the results that are passed in.
$old_users_id = [old('user_id')];
$old_value_users = User::whereIn('id', $old_users_id)->get();
If I replace $old_users_id = [old('user_id')] with $old_users_id = [1,2,3], it works like a charm. I have dd(old('user_id')) and it is indeed an array, so I do not understand what is the hick-up!
Since old('user_id') is an array, you should not put it in an array. Try this;
You shold provide an empty array if old('user_id') is null.
$old_users_id = old('user_id')?old('user_id'):[];
$old_value_users = User::whereIn('id', $old_users_id)->get();
+ point
Instead of whereIn try with whereIntegerInRaw if you are adding a large array of integer bindings to your query.For more check documentation
Something like this one
<select class="custom-select chosen-select {{ $errors -> has('add_project_owner') ? 'is-invalid' : '' }}" name="add_project_owner[]" id="add_project_owner" multiple>
<option value="">choose</option>
#foreach ($owners as $owner)
<option value="{{ $owner -> emp_name }}" {{ (collect(old('add_project_owner'))->contains($owner -> emp_name)) ? 'selected' : '' }}>{{ $owner -> emp_name }}</option>
#endforeach
</select>

Auto filter without using button and ajax in Laravel

I have 3 tables:
Type: id, name
Level: id, name
Question: id, content, type_id, level_id
I've done filter function with "Filter" button. But now, what I want is that when I filter, data will be loaded into table automatically without using button. How can I do?
Here is my code using button
FilterController:
public function filter(Request $request){
$types = Type::all();
$levels = Level::all();
$model = Question::where('id', '>', 0);
if (isset($request->type))
$model = $model->where('type_id', $request->type);
if (isset($request->level))
$model = $model->where('level_id', $request->level);
$data = $model->paginate(999)->appends(request()->query());
return view('filter', compact( 'data', 'request', 'types','levels'));
}
filter.blade.php
<form method="get">
<select name="type">
<option value="">All Types</option>
#foreach ($types as $item)
<option value="{{ $item->id }}" #if ($request->type == $item->id) selected #endif>{{ $item->name }}</option>
#endforeach
</select>
<select name="level">
<option value="">All Levels</option>
#foreach ($levels as $item)
<option value="{{ $item->id }}" #if ($request->level == $item->id) selected #endif>{{ $item->name }}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary">Filter</button>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>Content</th>
</tr>
#foreach ($data as $q)
<tr>
<td>{{ $q->id }}</td>
<td>{{ $q->content }}</td>
</tr>
#endforeach
</table>
You could simply submit the form when your select values change:
<select onchange="this.form.submit()">
...
</select>
Or if you didn’t want to use a form, assuming your path is /questions and you've set the APP_URL property in your .env file, you could do something similar to the following:
<select name="type" onchange="window.location.href = this.value">
<option value="{{ url('questions') }}">All types</option>
#foreach($types as $type)
<option value="{{ url('questions') }}?type={{ $type->id }}"{{ request('type') == $type->id ? ' selected' : '' }}>
{{ $type->name }}
</option>
#endforeach
</select>
If you want the currently selected level to stay selected when you filter by type, you could change your option value to the following:
<option value="{{ url('questions') . '?' . (request()->filled('level') ? 'level=' . request('level') . '&' : '') . 'type=' . request('type') }}">
{{ $type->name }}
</option>
Answered on my phone so there may be broken syntax.

dropdown list of html with $variable giving wrong result in display

I am trying to make dropdown-list in html/bootstrap which is result me wrong designed output. let me share what i have done in code.
controller code :
$countries = Country::select('country','id')->get();
HTML code:
<div class="form-group">
Country:<select name="country_id" id="exchange" class="form-control" for="id">
<option value="" id="fid"></option>
#foreach($countries as $key=>$val )
<option value="{{ $key}}">{{ $val }}</option>
#endforeach
</select>
</div>
problem you can see in below output:
here is output
Try my snippet, because $country in the loop holds a model object from Country
<div class="form-group">
Country:<select name="country_id" id="exchange" class="form-control" for="id">
<option value="" id="fid"></option>
#foreach($countries as $country )
<option value="{{ $country->id }}">{{ $country->country }}</option>
#endforeach
</select>
</div>
Change your controller code as follow
$countries = Country::pluck('country', 'id');

Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen

I have a dropdown called designation, where a user can add some information against multiple designation. If I add 1 record against 3 designation, then I need to select those during validation and edit time also.
Ex: Choosed id's {5,7,8} from [1 to 10].
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}" {{ old('forWhom',$info->forWhom) == $key ? 'selected' : ''}} />{{ $value }}</option>
#endforeach
</select>
After add of those information I store those selected id's in comma(,) separator i.e 5,7,8.
How can I make select this in laravel 5.4
After playing around a bit, I got the result.
Here is the piece of code.
During Add
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} />
{{ $value }}
</option>
#endforeach
</select>
During Edit
Suppose you got the result of selected ids in
$info->forWhom
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
/>
{{ $value }}
</option>
#endforeach
</select>
I hope this will help some one else.