Multiselect box keep database values as selected Laravel 5.8 - php

I am using the multi-select box as follows, where I want to keep old values as selected in edit mode.
{!!Form::select('team_id[]',$teams , old('team_id[]') , array('class'=>'custom-select', 'id'=>'team_id', 'multiple'=>true)) !!}
Below is my controller code.
$teams = Teams::select('team_name', 'id as team_id')
->where('team_status_id', 1)
->pluck('team_name', 'team_id');
$teams->prepend('Select Teams', 0);
I have 2 tables. One is teams table, which contains the name of all teams. Other is team_applied table which contains team_id and survey_id. In add form, I want to display all team name in the multi-select box, and in edit mode, I want to show all team ids in team_applied table as selected along with other unselected team names.
Below is teams table:
Below is team_applied table:
Here is my select box which I need values as selected:

For showing multiple item selected at the time of edit, try something like this:
Form::select('size[]',['L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], ['S', 'M'], ['multiple' => 'multiple', 'class' => 'form-control']);
// here ['S', 'M'] is the array that we have to show selected
Reference

Related

cant get the group of an int field from a query in cakephp3

I have a table with a field called 'year'. It has many repetitions in the column so I want to find the distinct group. I have 4 different 'years' in about 20 rows and I dont get these values from the query. Instead what is returned are 4 numbers which are not the years (5,14,4,70). The same code worked fine when I used this with suburb field in another table where there were multiple values of this field. I dont get why this isnt working.
//in view
echo $this->Form->input('year', ['label' => 'Year','options' => $allyears]);
//controller
$allyears = $this->TimesheetDates->find('list')
->select(['TimesheetDates.id', 'TimesheetDates.year'])
->group(['TimesheetDates.year'])->autoFields(true)
->order(['TimesheetDates.year'=> 'ASC'])
->hydrate(false);
$this->set('allyears',$allyears);
//another controller and this code worked fine
$suburb = $this->Students->find('list')->where(['Students.address_suburb !=' => '','Students.student_inactive' => 0])
->select(['Students.id','Students.address_suburb'])
->group(['Students.address_suburb'])->autoFields(true)
->order(['Students.address_suburb' => 'ASC'])
->hydrate(false);
take a look at the documentation about how find('list') works
$allyears = $this->TimesheetDates->find('list', [
'keyField' => 'id',
'valueField' => 'year']
)
->group(['year'])
->order(['year'=> 'ASC']);
Note that it has no meaning selecting the id of the TimesheetDates Table as you are grouping by year and the id is choosen randomly between all the records that share the same year

Codeigniter - dropdown change to multiple select because use array as the name

I have problem with my dropdown. I need dropdown that can select only one value and I use the same dropdown to repeat the selection for others row. Here is my page look like
Click here to view My page
My code for the dropdown is
<?php
for($i=0;$i<=$rNumber;$i++){
$occNo = array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'10' => '10'
);
echo form_dropdown('roomPax[]',$occNo,set_value('roomPax', isset($databaseData[$i]->roomPax) ? $databaseData[$i]->roomPax : ''),'class="form-control"');
}
?>
I found that, the dropdown change to multiple select because I use roomPax[] (array name). I have to use array name because the same dropdown I use for other row. How to set this dropdown as single select and maintain roomPax[] as their name?
You do not need to define name as like that: roomPax[]
Just replace it with roomPax in first param of form_dropdown()
From Codeigniter User Guide:
Lets you create a standard multiselect field. The first parameter will
contain the name of the field, the second parameter will contain an
associative array of options, and the third parameter will contain the
value or values you wish to be selected. The parameter usage is
identical to using form_dropdown() above, except of course that the
name of the field will need to use POST array syntax, e.g. foo[].
As per user guide, when you need multiselect options than you can use your select name as an array.

MySQL result multiple arrays

i.e : i have 2 tables
Product ( id, name )
Photo ( id, name, photo_id )
And I need to get result in array like this:
array(
'id' => 1,
'name' => product,
'photos' => array(
array('id' => 1, 'name' => 'photo1')
array('id' => 2, 'name' => 'photo2')
)
}
Is it possible in PHP using clear SQL?
I know that is possible to get 2 arrays and connect it but I have many records and I dont want to wase time to quering.
You have to add a foreign_key in your photo table "product_id".
Then create a method getPhotos() in your Product class with will collect all photos for your product.
Is it possible in PHP using clear SQL?
Not in a single SQL call. With a single call, this is the closest you can get:
array(
'id' => 1,
'name' => product,
'photo_id' => 1,
'photo_name' => 'photo1')
),
array(
'id' => 1,
'name' => product,
'photo_id' => 2,
'photo_name' => 'photo2')
)
Your only choice for the format you want is to run queries separately or to combine them into the data structure you want.
As mentioned, this is not possible with SQL. SQL is based on the relational model which is a 1-Normal-Form data model. That means, the result relation is also flat (no nested relations in a relation).
However, there are good frameworks which generate intermediary models in your corresponding target language (e.g. Python, Java, ...) that circumvent the impression of a flat data model. Check for example Django.
https://docs.djangoproject.com/en/1.8/topics/db/models/
Moo

GroceryCRUD : How to set field dropdown

I just code with GroceryCRUD,
This is my Database:
a_guest_data
guest_no
register_date
name
gender
birthday
address
city
country
phone
email
ref
status
date_modified
a_table_data
id_table
tbl_name
group_name
status
seat
party_date
locked
pos_tbl
date_modified
a_table_group
id_table
guest_no
priority
This is my Code for the Controller:
public function guest_management()
{
$crud = new grocery_CRUD();
$crud->set_table('a_guest_data');
$crud->set_subject('Guest');
$crud->set_relation_n_n('tables', 'a_table_group', 'a_table_data', 'guest_no', 'id_table', 'tbl_name');
$crud->unset_columns('register_date','layout_no', 'date_modified', 'ref');
$crud->fields('name', 'register_date', 'gender', 'birthday', 'tables', 'address', 'city', 'country', 'phone', 'email', 'ref', 'status' );
$crud->field_type('country','dropdown', array('Indonesia' => 'Indonesia', 'Others' => 'Others'));
$crud->field_type('gender','dropdown', array('Male' => 'Male', 'Female' => 'Female'));
$crud->field_type('status','dropdown', array('1' => '1', '2' => '2'));
$output = $crud->render();
$this->_example_output($output);
}
And I have the form like this
In the field Tables, I want to set the dropdown fields that only can choose 1 table (The Fact is, now in the field Tables, I can choose many options table. *See in the picture, I choose Table 1, 45, 6, 34).
How can I do that?
This behavior is due to your database structure. If you only want one table value you need your database structure to be "1-n database relation"
Documentation for about database relationships
What you want to achieve in grocery crud terms is set_relation.
Syntax for set_relation:
void set_relation( string $field_name , string $related_table, string $related_title_field [, mixed $where [, string $order_by ] ] )
Docs for set_relation
In the end your priority field will also be in the table a_guest_data
You are using a multiple select input, try removing MULTIPLE markup.
It depends on what type of select you are using.

Optional belongsTo relationship in cakephp

I have a simple Category model in my CakePHP application. I want to add sub-categories, and do this by simply adding a parent_id column.
The parent_id is a belongsTo relationship, that references back to the same Category model.
When I generate my admin scaffolds, a dropdown will shop up (correct!), but I'd like to include a NULL option in this dropdown (for top-level categories).
Is this possible? And if so, how..
(Note: not interested in Tree behaviour right now)
Sounds like you may be looking for the empty option:
<?php echo $form->input(
'field',
array( 'options' => array( 1, 2, 3, 4, 5),
'empty' => 'Select one'
); ?>
http://book.cakephp.org/view/201/options-empty

Categories