Eloquentize logo
Eloquentize

Usage

Counting eloquent models

The main commands of eloquentize is models-count, if you call it without any arguments, all models created today will be counted and the result will be stored in eloquentize.

php artisan eloquentize:models-count

Counting a specific date

The first argument of eloquentize:models-count can be a date in format DD/MM/YYYY, that will scope the created_at column from the date you gave between 00:00:00 and 23:59:59.

php artisan eloquentize:models-count 01/01/2024

The basic of a daily report is to schedule a models-count after midnight ( 00:01 ) to get everything created yesterday. So you can call models-count with “yesterday” as first argument

php artisan eloquentize:models-count yesterday

Retrieves records based on the specified timestamp column

By default all computations are done on created_at column, but you change this by setting the option “event”

php artisan eloquentize:models-count 01/01/2024 --event=updated_at

With this option set to updated_at you will get all the models that have been updated on the specified day. Be aware that if a model is updated twice or more in a single day you will only get one count for this.

If you used soft delete on your eloquent models, you can count eloquent model deletions like this.

php artisan eloquentize:models-count deleted_at

if you used a custom datetime column name liked signed_at you can also perform the models-count command on it.

php artisan eloquentize:models-count signed_at --models=Contract

To filter the models, use the -M or -models argument and pass a list of Models separated by commas.

Changing the date format

default format is DD/MM/YYYY, you can pass MM/DD/YYYY with the option -dateFormat

php artisan eloquentize:models-count 01/31/2024 --dateFormat="MM/DD/YYYY"

Specify models

You can only get metrics on a retrained list of models with comma separated values and the option —models

php artisan eloquentize:models-count 01/01/2024 --models=User,Bill

Custom models path

If your model are store in a specific folder you can use —modelsPath option starting from /app

php artisan eloquentize:models-count --modelsPath=/Custom/Path

Target Model property

php artisan eloquentize:property-aggregate Bill sum amount yesterday

Available Aggregations

eloquentize:property-sum

eloquentize:property-avg

eloquentize:property-min

eloquentize:property-max

Using Scope

You can apply scope to eloquentize models-count and property-aggregate. Because property-agregate target only one model you can call it straight forward like this

php artisan eloquentize:property-aggregate Bill sum amount yesterday --scope=priceOver --scopeValue=2000

Because models-count target multiple models, you have to specify the model

php artisan eloquentize:models-count -v --models=Bill --scope=PriceOver --scopeValue=1000

Schedule the commands

To automate the process, you need to setup App\Console\Kernel with the commands you need

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('php artisan eloquentize:models-count')->daily();
    }

So, this is the command to schedule in your kernel to get the basic statistics, namely the number of each model created over the 24 hours of the previous day by your application.

Typical Schedule

Here is a typical schedule you can find on a laravel project with eloquentize

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
		$schedule->command('php artisan eloquentize:models-count')->daily();
		$schedule->command('php artisan eloquentize:property-aggregate Bill sum amount ')->daily();
		$schedule->command('php artisan eloquentize:property-aggregate Bill avg amount ')->daily();
    }

Typical Schedule on Laravel 11

Laravel 11 introduce new schedule syntax, in routes/console.php


<?php
// routes/console.php
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Schedule;

Schedule::command('eloquentize:models-count')->hourly();
Schedule::command('eloquentize:property-aggregate Bill sum amount')->hourly();

About the author

© Eloquentize 2024