And what is the problem? It's never run? Is the scheduler running? https://laravel.com/docs/9.x/scheduling#running-the-scheduler
Apr 13, 2022
31
Level 1
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');
}
}
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
Please or to participate in this conversation.