I have a form in Laravel 5.1 that uses a dropdown menu that is populated form a db table. When the form is in edit mode I want the dropdown menu item to be selected that matches the id that is already in the table in addition to the other results from the db table.
Company table
array([ 'id' => 1, 'name' => 'ABC Company', 'lead' => 2]);
Lead table
array([
array(['id' => 1, 'lead_name' => 'Yellow Pages']),
array(['id' => 2, 'lead_name' => 'Internet']),
array(['id' => 3, 'lead_name' => 'Other'])
]);
CompanyController.php
public function edit($id)
{
$data = Company::where('id', $id)->first();
$leads = Lead::lists('lead_name', 'id');
return view('company.edit', compact('data', 'leads'));
}
company/edit.blade.php
{!! Form::model($data, ['method' => 'PATCH', 'action' => ['CompanyController#update', $data->id], 'class' => 'form-horizontal']) !!}
{!! Form::select('leads', $leads, null, ['class' => 'selectpicker']) !!}
What I'm trying to accomplish is when I edit id '1' I want the dropdown menu to show id '2' selected with he value of 'Internet' and then obviously the user can select the other 2 options and then update the table like normal.
Is it possible to use model binding in addition to a dropdown menu? Currently the menu populates but does not have id 2 selected.
1) Did you close the form properly with Form::close ?
2) It seems your $leads array may be backward. Should it not be?:
$leads = Lead::lists('id', 'lead_name');
Related
I got an issue. I have 3 select inputs, that i want to fill in with same options. First input gets options, but another two don't. I've tried everything. Last thing, that i tried was select 3 different queries and fill each one separately. Unfortunately, i get same issue.
Thanks for advices.
Controller
$dataPbxObj1 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj2 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj3 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbx1 = ArrayHelper::map($dataPbxObj1,'id','popis');
$dataPbx2 = ArrayHelper::map($dataPbxObj2,'id','popis');
$dataPbx3 = ArrayHelper::map($dataPbxObj3,'id','popis');
View (all this selects are same)
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(),
["data" => $dataPbx3,'hideSearch' => true]) ?>
You probably needs to use unique IDs - by default Yii generates ID based on field name, but with 3 identical fields, IDs will be the same, and Select2 init will apply only for first of them.
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX1'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX2'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX3'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
BTW: you don't need to query options list 3 times, you can do it once and use the same result in 3 fields.
I am using cakephp 3.3
I have in the controller
$employee = TableRegistry::get('employees');
$allNames = $employee->find('list', array('employee_name' => array('employee_name') ) );
$allNames = $allNames->toArray();
$this->set('name', $allNames);
and in my template
<?= $this->Form->input('employee', array('type'=>'select','label' => false,
'options' => $name,'value'=>$name));?>
The only thing being retrieved and displayed is the number of entries i have in the database, $name only contains an array(1,2,3,4) when it should be actual people names.
Did you read the documentation about how find('list') works?'
$allNames = $employee->find('list', [
'keyField' => 'employee_name'
'valueField' => 'employee_name'
]);
$this->set('name', $allNames);
<?= $this->Form->input('employee', ['type'=>'select','label' => false,
'options' => $name]);?>
You can map id and value by keyField and valueField in list query
in your controller
$employee = TableRegistry::get('employees');
$allNames = $employee->find('list', array('valueField' =>employee_name','keyField'=>'employee_id' )); // correct employee_id with your table id field.
$this->set('name', $allNames);
In your View
<?= $this->Form->input('employee', array('type'=>'select','label' => false,
'options' => $name));?>
I am making an edit page of my form and I have a select dropdown but I need to set selected value for that coz it is an edit form. I am doing it like this:
public function edit($id)
{
$form = Form::find($id);
$agent_options = array('' => 'Choose One') + DB::table('agents')->lists('name','id');
$campaign_options = array('' => 'Choose One') + DB::table('campaigns')->lists('name','id');
$query = "SELECT a.id, a.form_id, a.metrics_id, a.response, b.response as responseoption, b.metrics_name, b.description, b.question, a.remarks FROM qcv.forms_responses a INNER JOIN metrics b ON a.metrics_id = b.id WHERE form_id = $id;";
$metric = DB::connection('mysql')->select($query);
return view('form.edit')->with(array('form'=> $form, 'agent_options' => $agent_options, 'campaign_options' => $campaign_options, 'metric' => $metric));
}
In my View
<div class="col-md-6">
{!! Form::select('agent_id', $agent_options, '',array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}
</div>
Let's use my $agent_options dropdown, that will pull agents list in my agents table, but in my view how do I set a default value selected based on agent id? Let's say
agend_id = 1
is selected by default.
you need to pass the default value to the select() as
{!! Form::select('agent_id', $agent_options, $defaultValue, array('class' => 'form-control', 'id' => 'agent_id', 'required' => 'required')) !!}}
replace the $defaultValue with the value you need to select by default.
refer the DOC
How can i get categories names to the select list not their ID's !! , I'm using CakePHP
the 'categories' table has only two columns (id, name)
the view part :
echo $this->Form->input('category',array(
'type' => 'select',
'options' => $categories,
'empty' => 'select category'
));
the association :
class Job extends AppModel{
public $name = 'Job';
public $belongsTo = array('Category');
}
and the controller :
$categories= $this->Job->Category->find('list');
$this->set('categories',$categories);
the result is a select list with 1,2,3,4,5,6 values
You needs to edit your controller code as following :
$categories= $this->Job->Category->find('list',array('fields'=>array('Category.id','Category.name')));
you need to use virtual field in cakephp.
http://book.cakephp.org/2.0/en/models/virtual-fields.html
also Line main in ctp file
foreach($times as $key => $value ){ $timesList[$value] = $value; } $times = $timesList
use as per your model-controller. for mor info. plz follow this link .here i face same problem and i got solution.
virtual field are not working in cakephp
In cakephp 3.X
// Common Usage:
$users = [
['id' => 1, 'name' => 'mark'],
['id' => 2, 'name' => 'jane'],
['id' => 3, 'name' => 'sally'],
['id' => 4, 'name' => 'jose'],
];
$results = Hash::extract($users, '{n}.id');
// $results equals:
// [1,2,3,4];
https://book.cakephp.org/3.0/en/core-libraries/hash.html
I create 2 arrays from a model, 1 being already selected values from the user, and the 2nd available values which the user can then also select. This is for an edit page. I want to populate a multi-select input box with the values of both models, but want the already chosen values (1st array) highlighted. It creates the models fine, and using array_merge() I merge both the arrays as the options, but the selected does not highlight the correct fields. Any tips?
// Controller:
$ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
$ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));
// In the view:
echo $this->Form->input('SurveyIvrNumbers.id',array(
'empty' => '-- Select IVR Number(s) --',
'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
'selected' => $ivrNumbersSelected,
'class' => 'textbox',
'multiple' => true,
'div' => array(
'class' => 'field'
),
'label' => array(
'class' => 'label-tooltip',
'title' => '', //tool tips
'text' => 'IVR Numbers: (you can select multiple numbers)'
),
'after' => '<p class="field-help"></p>'
));
If you set $this->request->data to the record you are currently editing CakePHP will automatically populate this data for you!
// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id);
// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all'));
// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array(
'empty' => '-- Select IVR Number(s) --', // plus other options
);