Laravel 5 - List database entries using where clause - mysql

I am trying to list all entries which fall under a where condition. If I do the following, I get all the entries returned
$users = User::lists('userName', 'id');
However, I am looking to return only the users who have a department id of 3. So I am doing
$users = User::lists('userName', 'id')->where('departmentId', 3);
However, this returns an empty result set. In my database, I do have users with this department id.
How can I get the lists statement working?
Just a note, the following returns the result I need
$users = User::select('userName', 'id')->where('departmentId', 3)->get();
However, in my edit form, because I have this
!! Form::select('csManager', $users, Input::old('users'), ['class' => 'csManager']) !!}
The old input is not selected and the data is showing up as an array. I know the way to fix this is to do my select like this
<select class="csManager" name="csManager">
#foreach($users as $user)
<option value="{{ $user->id }}">{{ $user->userName }}</option>
#endforeach
</select>
But then I am not sure how to display the old user within the above select.
Any help appreciated.
Thanks

To mark a old selection try this:
<option value="{{ $user->id }}"
{{ (Input::old('users') == $user->id)? 'selected' : '' }}>
{{ $user->userName }}
</option>
As #TimVanUum say, you can save a little bit of code if you prefer. Both are equals.
<option value="{{ $user->id }}"
{{ (Input::old('users') !== $user->id)?: 'selected'}}>
{{ $user->userName }}
</option>

In your controller:
$users = User::where('departmentId', 3)->lists('userName', 'id');
In blade form:
{!! Form::select('csManager', $users, null, ['class' => 'csManager']) !!}
To get the old data in edit form instead of
{!! Form::open([your attributes]) !!}
Use model binding. something like this:
{!! Form::model($user,[your attributes]) !!}
In this case your edit method should have $user=findOrFail($id);

Related

How to fetch value of multiselect dropdown in Laravel 8?

I have this code for multiselect dropdown
<select id="kit" name="tool_id[]" multiple class="form-control single-select selectpicker">
<option value="">Select Tool Name</option>
<?php foreach($tools as $tool){?>
<option value="<?php echo $tool->id;?>"><?php echo $tool->name;?></option>
<?php }?>
</select>
I can insert data in database ,but unable to retrieve on show blade
value stores in database like this in array
Help me to retrieve array value from MySql DB
Retrieve array value from data base in laravel
Since you are storing an array, you can use in_array to achieve the result you are looking for:
// I will assume here that you are passing a list of IDs from view
// I think in your example its "tool_id"
<option value="{{$tool->id}}" {{in_array($tool->id, $listOfIds) ? 'selected' : ''}}>{{$tool->name}}</option>
You can read more examples from https://www.w3schools.com/php/func_array_in_array.asp
Also you do not need to call <?php //code ?> in blade files, you can simply use {{ //code }} to render it, you can read more on blade syntax from https://laravel.com/docs/9.x/blade
Good luck!
If you can fetch the value of tool_id from the database, use this.
{!! Form::select('tool_id', $yourOptionArray, $toolIds, ['class' => 'form-control', 'multiple' => 'multiple']) !!}
By default, this will add selected keywords to 11 and 13.
As the value of the select tag is being saved as an array, this can be simply done with the in_array condition as mentioned below:
<option value="{{ $tool->id }}" #if(in_array($tool->id,$IDsList)) selected #endif>{{ $tool->name }}</option>
Assuming the $IDsList (passed to the view blade from controller) have the value stored for the $tool->id

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>

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();

populate data from database into select dropbox

I have a select box which needs to fetch from the database.
view blade:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($service as $sn)
{
<option value="">dd($sn)</option>
}
#endforeach
</select>
controller:
public function index()
{
$service = DB::select('select service_type from services');
return view("service",['service'=>$service]);
}
I am getting an error like
Undefined variable: service (View: /home/devadmin/.config/composer/vendor/laravel/installer/serenseprj/resources/views/package.blade.php)
You can use Eloquent instead of doing this
$service = DB::select('select service_type from services');
Using Eloquent,
$services = ModelName::pluck('name', 'id')->get();
You can pass the variable to the targeted view like below:
return view('view_name', ['variable_name' => $variable]);
And, you can access the variable name like below
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($variable_name as $var)
{
<option value="{{$var->id}}">{{$var->name}}</option>
}
#endforeach
</select>
You can use this code as the references. Hope you have service model.
$services= Service::pluck('name', 'id');
return view('services', compact('services'));
then in view file you can use laravel collevtives like.
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'service Id:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control'])!!}
</div>
you can use this too. in you blade file
<select name="service " id="service " class="form-control">
<option value=""> -- Select One --</option>
#foreach ($services as $service )
<option value="{{ $service->id }}">{{ $service->name }}</option>
#endforeach

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.