Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate drop-down list with database values in CakePHP

Tags:

cakephp

I am very new to CakePHP. Could you please explain me the steps needed to populate a select drop-down with values from the database. Please also suggest me some links to the reference.

like image 472
n92 Avatar asked Dec 21 '22 17:12

n92


1 Answers

Simple, if it's a related model in your controller you pass 'list' into the find(); an cake will make an id => value array for you, and the form helper will know exactly what to do with it.

For example say you want to get the list of categories for a product model, this is in your contoller:

$categories = $this->Product->Categories->find('list');
$this->set(compact('categories'));

Then in your view using the form helper, simply create the select element how you normally would any input:

$form->input('category_id');

The form helper will automatically load the $categories variable we set with $this->set().

like image 109
Dunhamzzz Avatar answered May 20 '23 09:05

Dunhamzzz