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

mehrabt's avatar

Create Daily Report With Laravel Schedule in Laravel

Hi guys, o Want to create Daily Report with jobs in Laravel I wrote this code but it doesn't work what is the problem?

this is my DailyReport.php:

    protected $signature = 'report:daily';

    protected $description = 'Create Daily Report';

    public function handle()
    {
        $admins=Admin::query()->get();
        foreach ($admins as $admin){
            Report::create([
               'date' => now()->toDateTimeString(),
               'user_id' => $admin->id,
               'time' => '00:00:00',
            ]);
        }
    }

and Karnel.php:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    protected $commands = [
        Commands\DailyReport::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('report:daily')
            ->dailyAt('21:16')
        ;
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

and this is my create_reports_table:


class CreateReportsTable extends Migration
{

    public function up()
    {
        Schema::create('reports', function (Blueprint $table) {
            $table->id();
            $table->date('date')->unique();;
            $table->longText('description')->nullable();
            $table->foreignId('user_id')->constrained('admins')->cascadeOnDelete();
            $table->time('time');
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('reports');
    }
}

0 likes
31 replies
sr57's avatar

it doesn't work

means what?

are you able o launch the job manually?

mehrabt's avatar

@sr57 my job must create a report at a specific time but it doesn't create any report at that time ...

sr57's avatar

@mehrabt

Ok we know that, but you did not answer to our ( @sinnbeck and me ) questions.

So my question

are you able o launch the job manually?

if yes , see @sinnbeck 's link

mehrabt's avatar

@sr57 yes, for test, i can launch the job on my xampp localhost...

mehrabt's avatar

@sr57 when i run this php artisan schedule:run command (with windows), it returns me

[2022-04-13T18:00:58+00:00] Running scheduled command: "C:\xampp\php\php.exe" "artisan" report:daily > "NUL" 2>&1

but it doesn't create those reports...

mehrabt's avatar

@Sinnbeck do you mean it runs 24 hours after running the php artisan schedule:run comment?( if daily_at is equal to now)

Sinnbeck's avatar

@mehrabt not sure what you mean. The schedule work command runs until you stop it. If the current time is the same as the scheduled time, the command is run. So this command will only be run when the local clock says 21:16

$schedule->command('report:daily')
            ->dailyAt('21:16')
        ;
mehrabt's avatar

@Sinnbeck I changed 21:16 to the current time, But my problem is that reports are not created at this time

mehrabt's avatar

@Sinnbeck do you mean with php artisan schedule:run ? no the report doesn't create with this command

Sinnbeck's avatar

@mehrabt no I mean running the command directly. I assumed you tested this as you answered this when @sr57 asked you

yes, for test,

But it seems you never tested it..

1 like
zhxscript's avatar

@mehrabt

1.) Your table is expecting a date, but you're sending a DateTime. (column date should maybe be date time, or change now()->toDateTimeString() -> now()->toDateString(),

2.) Read your error logs, if the report is working, but erroring on creating, it will tell you why, what do your logs say?

mehrabt's avatar

@zhxscript 1- in Laravel Requests, i Used ->toDateTimeString() for default value and it works. but anyway, i changed change now()->toDateTimeString() to now()->toDateString() but it still doesnt work

2-i don't have any activity log

zhxscript's avatar

@mehrabt

Try this: $schedule->command('report:daily');

Then make sure you run this command from the root of your project php artisan schedule:run

Let me know the output of the command and or if new log entries have been made.

mehrabt's avatar

@zhxscript command php artisan schedule:run returns me this output:

[2022-04-14T15:17:27+00:00] Running scheduled command: "C:\xampp\php\php.exe" "artisan" report:daily > "NUL" 2>&1
Snapey's avatar
Snapey
Best Answer
Level 122

Forget the scheduler.

Does the report get created when you run php artisan report:daily

When this works, you can think about scheduling it

2 likes
mehrabt's avatar

@Snapey Thank you so much. 🌺 it returns me

 SQLSTATE[HY000]: General error: 1364 Field 'description' doesn't have a default value (SQL: insert into `reports` (`date`, `user_id`,
 `updated_at`, `created_at`) values (2022-04-14, 1, 2022-04-14 15:19:49, 2022-04-14 15:19:49))

and I can fix the problem ...

but the job must work without description value because the description is nullable πŸ€”πŸ€”πŸ€”

mehrabt's avatar

it work if i add this 'description' => '', (empty value) into create

Thank youπŸ™

Snapey's avatar

This might be of interest.

kernel.php

    // other scheduled commands
    $this->sendReports($schedule);
   // other scheduled commands

   //

    public function sendReports($schedule)
    {
        $reports = Report::get(['id','frequency']);

        $reports->each(function($report) use($schedule){
            $schedule
                ->command('send-report ' . $report->id)
                ->cron($report->frequency);
        });
    }

I have a table of reports. The report has a report number. The specific report is run by calling artisan send-report and passing the ID of the report to run

The report is run whenever the frequency on the report matches the current time.

So in the 'frequency' field, I store a cron pattern such as 00 12 * * 7 which means run the report at midday on sunday, or a report might have 0,30 * * * * which means run at 00 and 30 minutes, every hour of every day.

The scheduler runs every minute so it checks the reports each minute and fires off any reports whose time is due.

mehrabt's avatar

it works when i run php artisan report:daily or php artisan scadule:run command but it does not run AUTOMATICALLY when I use ->everyMinute()

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('report:daily')
            ->everyMinute()
        ;
    }
Sinnbeck's avatar

@mehrabt you need to run a scheduler. It was the very first thing I posted

1 like
Snapey's avatar

use the command php artisan schedule:work

It will run every minute.

However, you are writing the date into the database, and have a unique constraint on the date column, presumably meaning that it can only store one record per date

1 like

Please or to participate in this conversation.