Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: Display multiple fields in a single drop down

$this->loadModel('Product');
$this->set('products',$this->Product->find('list',array('product'=>array('products.name' => 'products.price'))));
$this->set(compact('products'));

I am trying to make it so I can display both the product name and it's price beside each other in the function I am using. So when the drop down is selected, instead of just being "productname" it would be like "productname - $price".

I am pretty sure this is all the code that needsto be seen.

like image 349
Web Owl Avatar asked Feb 18 '26 18:02

Web Owl


1 Answers

Simple add the following line in your Product Model:

public $virtualFields = array('name_price' => 'concat(Product.name, "-", Product.price)');

and try the following code to fetch:

$this->loadModel('Product');
$this->set('products',$this->Product->find('list',array('fields'=>array('Product.name_price' => 'Product.price'))));
$this->set(compact('products'));
like image 178
Arun Jain Avatar answered Feb 21 '26 15:02

Arun Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!