laravel retrive old() wherein query values - mysql

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>

Related

Old Value/Search Value on search select button in Laravel

i make a select button for search specific data on my table.
but there is problem when i click the button, the button doesn't remain or stay from past request value that i click, or it reset back to select all
so this is my form in my blade file
<form action="">
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
#if(old('rumah_id') == $rumah->id)
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#else
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#endif
#endforeach
</select>
</form>
this is my Controller
public function index(Request $request){
$rumah = Rumah::all();
$mobil = Mobil::with(['rumah'])->orderBy('rumah_id', 'asc')
->when($request->rumah_id != null, function($q) use($request){
return $q->where('rumah_id', 'like', '%'. $request->rumah_id. '%');
})
return view('mobil/index',[
'mobils' => $mobil,
'rumahs' => $rumah,
]);
}
also this is my migration database for rumah look like
Schema::create('rumahs', function (Blueprint $table) {
$table->id();
$table->string('alamat');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
Try this :
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
<option value="{{ $rumah->id }}" {{ old('rumah_id') == $rumah->id ? 'selected' : '' }}>{{ $rumah->alamat }}</option>
#endforeach
</select>

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!!

Inserting data from <option> to Database in Laravel 5.8

I'm trying to insert data into a database with an option menu.
<select class="form-control" name="tim">
#foreach ($tim as $t)
<option value="{{ $t->id }}">{{ $t->name }}</option>
#endforeach
</select>
and this the controller
$data=new Modelverif();
$data->id_profil=$request->id;
$data->$id_pegawai=$request->get('tim');
$data->save();
how to input database with option menu with array?
Blade.php
<select class="form-control" name="tim">
#foreach ($tim as $t)
<option value="{{ $t->id }}">{{ $t->name }}</option>
#endforeach
</select>
the controller
$data=new Modelverif();
$data->id_profil=$request->id;
$data->$id_pegawai=$request->tim; //Or $data->$id_pegawai= $request->input('tim');
$data->save();
I think for this you need to convert your select into multiple choice.
<select class="form-control" multiple name="tim">
it will allow you choose mulitple values from select and give an array of values when you post the data
try this one:
$data=new Modelverif();
$data->id_profil=$request->id;
// assuming you have column name in $id_pegawai
$data->$id_pegawai= $request->input('tim');
// or,
// $data->$id_pegawai= $request->tim;
$data->save();

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.

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.