jeudi 14 novembre 2019

Add Select Get Value

I want to get order status name based on order_status_id

I have order statuses in the array:

$orderStatuses = [
    1 => 'Waiting',
    2 => 'Delivered',
    3 => 'Rejected'
];

I need to get the order status name and order_status_id.

$orders = Order::limit(50)->get();

I am now only getting order status name from looping through each order like:

foreach($orders as $order){
    $order->order_status_name = $orderStatus[$order->order_status_id];
} 

Is there any way to get the order status name without using foreach.

What I tried:

Order::addSelect("$orderStatuses[".'order_status_id'."]". " as order_status_name");

I am unable to get the result. Is there any way to get the result from the eloquent query or do I have to use foreach?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire