Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

seularts's avatar

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!

0 likes
6 replies
seularts's avatar

Sorry, I am new to this forum, I do not know how to past the html code to show correctly!

gerardnll's avatar

Haven't tried it but, how about using old('user_id[]')

seularts's avatar

dd(old('user_id[]')) It passes as null if I write it this way. The array shows up when I do dd(old('user_id')).

seularts's avatar

Ok, the fix was here: $old_value_users = User::whereIn('id', old('user_id') ?? [])->get();

1 like
seularts's avatar

Actually, an even better approach is to use whereIntegerInRaw for large queries!

Please or to participate in this conversation.