Comparison of hours in Laravel

5

I'm trying to create a method that will return the amount of minutes since the last insertion in the database. The way I did it was this one, but this one giving error:

$now = Carbon::now();
$minutes = LusLeadUpdateStatus::select('created_at')
     ->orderby ('created_at', 'desc')
     ->get()
     ->toArray();

$totalDuration = $minutes->diffInMinutes($now);
(// o erro é aqui, pois dando print_r($minutes) o resultado é algo assim:
    [0] => Array
    (
        [created_at] => 2018-08-30 14:55:35
    )

    [1] => Array
    (
        [created_at] => 2018-08-30 14:55:20
    )
)

The created_at field is where I have the insert in the database, and it is in the date / time format.

    
asked by anonymous 30.08.2018 / 21:38

1 answer

6

This was the final answer I got (working correctly):

$lusLeadUpdateStatus = LusLeadUpdateStatus::select('created_at')
            ->orderby('created_at', 'desc')
            ->first();

        if ($lusLeadUpdateStatus) {
            $now = Carbon::now();

            $lastDateTime = $lusLeadUpdateStatus->toArray();
            $lastDateTime = $lastDateTime['created_at'];

            return $now->diffInMinutes($lastDateTime);
        }
        return false;
    
30.08.2018 / 22:15