Commands and options
Every command starts with php artisan eloquentize:. This page lists what each one does, then shows how to change what it counts.
Add -v to any command to see the details. Add --dry to see what would be sent without sending it.
The commands
| Command | What it does |
|---|---|
models-count |
Counts models for one day. This is the one you will use most. |
property-aggregate |
Sums, averages or finds the min or max of a model property for one day. |
models-count-legacy |
Counts models for every past day. |
property-aggregate-legacy |
Aggregates a property for every past day. |
model-count-overall |
Counts one model over all time. |
property-aggregate-overall |
Aggregates a property over all time. |
Count your models
php artisan eloquentize:models-count
With no option, this counts today's records for every model in app/Models and sends the result.
Choose the day
The first argument is the day to count. It can be today (the default), yesterday, or a date written as DD/MM/YYYY:
php artisan eloquentize:models-count yesterday
php artisan eloquentize:models-count 31/01/2024
The command counts from 00:00:00 to 23:59:59 of that day.
To write dates another way, pass a PHP date format with --dateFormat:
php artisan eloquentize:models-count 01/31/2024 --dateFormat="m/d/Y"
php artisan eloquentize:models-count 2024-01-31 --dateFormat="Y-m-d"
Choose the date column
By default, Eloquentize looks at created_at. Use --event to look at another column:
php artisan eloquentize:models-count --event=updated_at
php artisan eloquentize:models-count --event=deleted_at
php artisan eloquentize:models-count --event=signed_at --models=Contract
updated_atcounts the records modified that day. A record modified several times in one day counts once.deleted_atcounts soft deletes.- Any other date column works, such as
signed_at, if the model has it.
Count only some models
List the models with --models (or -M), separated by commas:
php artisan eloquentize:models-count --models=User,Bill
Use the class name only, without the namespace.
Models in another folder
By default, Eloquentize reads app/Models (or app if that folder does not exist). If your models live elsewhere, give the folder relative to app, with no leading slash:
php artisan eloquentize:models-count --modelsPath=Custom/Path
This reads the models in app/Custom/Path. They must be directly in that folder, not in a subfolder, and their namespace must be App\Custom\Path.
Sum, average, min or max a property
Use property-aggregate with the model, the property, the aggregation and, optionally, the day:
php artisan eloquentize:property-aggregate Bill amount sum yesterday
This adds up the amount of every Bill created yesterday.
The aggregation is one of:
| Aggregation | Result |
|---|---|
sum |
The total |
avg |
The average |
min |
The smallest value |
max |
The largest value |
The day, --event, --dateFormat and --modelsPath work like they do for models-count.
Use a scope
A scope lets you count only some records, for example the bills over a given price. Pass the name of your scope with --scope, and its argument with --scopeValue:
php artisan eloquentize:property-aggregate Bill amount sum yesterday --scope=priceOver --scopeValue=2000
property-aggregate targets one model, so it needs nothing more. models-count targets several, so you must say which model has the scope:
php artisan eloquentize:models-count --models=Bill --scope=priceOver --scopeValue=1000
Schedule your commands
Put the commands in your scheduler, so they run without you.
A typical setup on Laravel 11 and later, in routes/console.php:
use Illuminate\Support\Facades\Schedule;
Schedule::command('eloquentize:models-count')->hourly();
Schedule::command('eloquentize:property-aggregate Bill amount sum')->hourly();
Schedule::command('eloquentize:property-aggregate Bill amount avg')->hourly();
On Laravel 10 and earlier, in app/Console/Kernel.php:
protected function schedule(Schedule $schedule): void
{
$schedule->command('eloquentize:models-count')->hourly();
$schedule->command('eloquentize:property-aggregate Bill amount sum')->hourly();
$schedule->command('eloquentize:property-aggregate Bill amount avg')->hourly();
}
Running every hour keeps today's numbers fresh. If you prefer one run per day, run it just after midnight and count the day that just ended:
Schedule::command('eloquentize:models-count yesterday')->dailyAt('00:05');