I want to change my array to number.

This is what I get "total_amount" => "817450.00"

but I only want the 817450.00

How to change to number but not array.

  $total_sale = CtAgent::where('agent_id', $agent_id)
    ->select('total_amount')
    ->orderByDesc('ct_id')
    ->limit('1')
    ->first();
    dd($total_sale);

show number

3 Answers

0
problem On Best Solutions
 $total_sale = CtAgent::where('agent_id', $agent_id)
    ->select('total_amount')
    ->orderByDesc('ct_id')
    ->limit('1')
    ->first()['total_amount'];
2
nakov On

You can simply use the attribute:

  $total_sale = optional(CtAgent::where('agent_id', $agent_id)
    ->select('total_amount')
    ->orderByDesc('ct_id')
    ->limit('1')
    ->first())->total_amount;
    dd($total_sale);

Using optional in case the query returns null so it won't throw an exception.

1
Vishal Tarkar On

Previous answers will work but here is a different method of doing the same, Which I prefer.

$total_sale = CtAgent::where('agent_id', $agent_id)
->orderByDesc('ct_id')
->pluck('total_amount')
->first();

& You don't have to handle if no records are found or value is null. ;)