Inserting data from <option> to Database in Laravel 5.8 - html

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

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>

Form query with pivot table

In form below my user share document access with other users.
How to increase my selector to show me only user who are not shared with this document yet? Document and User has pivot table document_user
<select type="text" name="user" class="uk-select">
<option disabled selected>Choose from contacts</option>
#foreach($company->users as $contact)
//#if (something)
<option value="{{ $contact->id }}">{{ $contact->first_name}} {{ $contact->last_name}}</option>
#andif
#endforeach
</select>
Document = $document
Tables:
user:
id
name
document:
id;
company_id
name
document_user:
- company_id
- user_id
Try this..
From your table structure I believe you should have the many to many relation.
User.php
public function documents()
{
return $this->belongsToMany(Document::class, 'document_user');
}
xx.blade.php
<select type="text" name="user" class="uk-select">
<option disabled selected>Choose from contacts</option>
#foreach($company->users as $contact)
#if(!in_array($contact->id, $document->users->pluck('id')->toArray()))
<option value="{{ $contact->id }}">
{{ $contact->first_name}} {{ $contact->last_name}}
</option>
#endif
#endforeach
</select>

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.

how to deactivate fetch value database to select box in laravel 4

Iam trying to edit student details.
iam fetch values from database. blood group fetch from database to display select box. and also display other blood groups in same select box.
select class="form-control" name="studbldgrp" id="studbldgrp">
option value="{{$student->studbldgrp}}">{{$student->studbldgrp}}</option>
option value="O+ve">O+ve</option>
option value="O-ve">O-ve</option>
<option value="A+ve">A+ve</option>
<option value="A-ve">A-ve</option>
<option value="B+ve">B+ve</option>
<option value="B-ve">B-ve</option>
<option value="AB+ve">AB+ve</option>
<option value="AB-ve">AB-ve</option>
<option value="Other">Other</option>
</select>
my problem is if im fetch value is A+ ,and display in select box. but it also in option value field.
i no need to again this value(A+)A +ve in select option field.
how to disable this field???
Create an array of your groups. Than iterate over it and don't print value matches $student->studbldgrp.
Array in php:
$groups = array(
'O+ve',
'O-ve',
'A+ve',
'A-ve',
'B+ve',
'B-ve',
'AB+ve',
'AB-ve',
'Other'
);
Blade template:
<select class="form-control" name="studbldgrp" id="studbldgrp">
<option value="{{$student->studbldgrp}}">{{$student->studbldgrp}}</option>
#foreach ($groups as $group)
#if ($group != $struden->studbldgrp)
<option value="{{ $group }}">{{ $group }}</option>
#endif
#endforeach
</select>