Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nkvolt's avatar

Calculation and sum in Eloquent

Hi everyone,

I 've got 2 tables, tbl_user which has 2 columns: user_id and user)name and table tbl_time in which has 3 columns: user_id, time_start and time_stop (time_start and time_stop is format as time). Please help me to build an Eloquent (Laravel 9.0) command to get the total minutes of all users which contains the following columns: user_name, time_start, time_stop, total_minute

Thank in advance.

0 likes
1 reply
LaryAI's avatar
Level 58

One possible solution to this problem is to use Laravel's Eloquent ORM to define relationships between the two tables and then use the sum method to calculate the total minutes for each user.

First, we need to define the relationships between the two tables in our models. In the User model, we can define a times method that returns a hasMany relationship to the Time model:

class User extends Model
{
    public function times()
    {
        return $this->hasMany(Time::class);
    }
}

In the Time model, we can define a user method that returns a belongsTo relationship to the User model:

class Time extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

With these relationships defined, we can now use Eloquent to query the database and calculate the total minutes for each user. Here's an example command that does this:

use App\Models\User;
use Illuminate\Console\Command;

class CalculateTotalMinutesCommand extends Command
{
    protected $signature = 'calculate:total-minutes';

    public function handle()
    {
        $users = User::with('times')->get();

        $data = [];

        foreach ($users as $user) {
            $totalMinutes = $user->times->sum(function ($time) {
                return $time->time_stop->diffInMinutes($time->time_start);
            });

            $data[] = [
                'user_name' => $user->user_name,
                'time_start' => $user->times->min('time_start'),
                'time_stop' => $user->times->max('time_stop'),
                'total_minute' => $totalMinutes,
            ];
        }

        $this->table(['user_name', 'time_start', 'time_stop', 'total_minute'], $data);
    }
}

In this command, we first load all users from the database along with their associated times using the with method. We then loop through each user and use the sum method to calculate the total minutes for that user. Finally, we build an array of data that includes the user's name, the earliest start time, the latest stop time, and the total minutes, and display it in a table using the table method.

Note that we use the diffInMinutes method to calculate the difference between the start and stop times in minutes. This method is provided by Laravel's Carbon library, which is included with Laravel by default.

To run this command, simply run php artisan calculate:total-minutes from the command line. The output will be a table showing the total minutes for each user.

Please or to participate in this conversation.