lundi 26 août 2019

How to use the value of array inside DB raw Laravel

Hi I have question about DB raw in Laravel. Actually now I have a variable named $currencies with the structure like this.

<?php

$currencies = [
    'CNY' => 1000,
    'USD' => 10000
]

And I need to use the value of that array for sorting my products table. With the products table looks like this:

| id  |  product_name  | original_currency  | price |
|  1  | lorem ipsum d  | CNY                | 20    |
|  2  | new product n  | USD                | 10    |

So far my code looks like this:


$products = DB::raw(SELECT product_name, 
                           price, 
                           price * $currencies[original_currency] AS converted_price 
                    FROM products);


But I got the error Undefined Index: original_currency

I just need to access the correct value to set the converted_price. So the expected result will looks like this:

| id  |  product_name  | original_currency  | price |  converted_price  |
|  1  | lorem ipsum d  | CNY                | 20    |  20000            |
|  2  | new product n  | USD                | 10    |  1000000          |



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30FJo4Y
via IFTTT

Aucun commentaire:

Enregistrer un commentaire