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

ilex01's avatar

Cron doens't work with blade.php file

Cron doesn't output anything with this code:

php -f /home/ceercle/ceercle/resources/views/cron.blade.php

cron.blade.php file:

<?php

use Illuminate\Support\Facades\DB;

$votes = DB::table('votes')
    ->selectRaw("website_id, SUM(stars) AS website_rating_sum")
    ->groupBy('website_id')
    ->orderByDesc('website_rating_sum')
    ->get();

$i = 0;
?>

@foreach($votes as $vote)

    <?php
    $i++;
    ?>

    <?php
    $getWebsiteName = DB::table('sites')
        ->select('id', 'website_name')
        ->where('id', $vote->website_id)
        ->first();
    ?>

    <?php
    $checkIfWebsiteExistsInDB = DB::table('classement')
        ->where('website_name', $getWebsiteName->website_name)
        ->first();

    if(!isset($checkIfWebsiteExistsInDB)) 
    {
        DB::table('classement')->insert([
            'website_name' => $getWebsiteName->website_name,
            'rank' => $i
        ]);
    }
    else 
    {

    }
    ?>

    Redirecting...

    <script>window.location = "/site?websitename={{ $getWebsiteName->website_name }}";</script>

@endforeach

I guess 'DB' (the laravel SQL queries) are not recognized and I should for usual SQL. Is there a way to keep the laravel file and make a cron on it?

0 likes
29 replies
Tray2's avatar

What is it that you are trying to do here?

ilex01's avatar

@Tray2 I try to execute the cron.blade.php script with cron and it doesnt work with the file (cron.blade.php) above.

Sinnbeck's avatar

@ilex01 3 things

  1. Why a blade file? Blade files are for rendering html mixed with php. Just use a regular php file
  2. Why do you have Javascript in a php script file?
  3. Why not use a command in laravel?
ilex01's avatar

@Sinnbeck Even if the file is renamed from cron.blade.php to cron.php and placed in the public_html folder it doens't work. Which command please?

Sinnbeck's avatar

@ilex01 Javascript is a browser. You are executing the file on the command line

Maybe try to explain the end goal

ilex01's avatar

@Sinnbeck It's because the file is called after submiting a form. Then you are redirected to /site?websitename=... But the file is also executed every minute with cron. And this is a redirection that we don't care in the case the file is executed by cron.

ilex01's avatar

@Sinnbeck I've an error with the command:

file: kernel.php

    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();

        $schedule->call(function () {

            $votes = DB::table('votes')
                ->selectRaw("website_id, SUM(stars) AS website_rating_sum")
                ->groupBy('website_id')
                ->orderByDesc('website_rating_sum')
                ->get();

        })->daily();
    }

command: php artisan schedule:list

[ceercle@hybrid1157 ceercle]$ php artisan schedule:list

   ErrorException

  DateTime::setTimezone() expects parameter 1 to be DateTimeZone, null given

  at vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleListCommand.php:42
     38▕                 $event->expression,
     39▕                 $event->description,
     40▕                 (new CronExpression($event->expression))
     41▕                             ->getNextRunDate(Carbon::now()->setTimezone($event->timezone))
  ➜  42▕                             ->setTimezone($this->option('timezone', config('app.timezone')))
     43▕                             ->format('Y-m-d H:i:s P'),
     44▕             ];
     45▕         }
     46▕

      +14 vendor frames
  15  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

   Whoops\Exception\ErrorException

  PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /opt/cpanel/ea-php73/root/usr/lib64/php/modules/imagick.so (libMagickWand.so.5: cannot open shared object file: No such file or directory), /opt/cpanel/ea-php73/root/usr/lib64/php/modules/imagick.so.so (/opt/cpanel/ea-php73/root/usr/lib64/php/modules/imagick.so.so: cannot open shared object file: No such file or directory))

      +1 vendor frames
  2   [internal]:0
      Whoops\Run::handleShutdown()
[ceercle@hybrid1157 ceercle]$
ilex01's avatar

@Tray2 Thank you but I'm lost with this code. I don't intend to check if the website is up.

Sinnbeck's avatar

@ilex01 sorry I cannot see how you have managed to break the schedule kernel then. Can you download laravel in another folder and see if it works there?

ilex01's avatar

@Sinnbeck I've the same error with a fresh new laravel:

Kernel.php

<?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.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();

        $schedule->call(function () {
            $foo = 'foo';
        })->daily();

    }

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

        require base_path('routes/console.php');
    }
}
php artisan schedule:list

See the screencapture: https://i.gyazo.com/bedbd506392c9ad825a1ad8339ecbc27.png

ilex01's avatar

It seems laravel needs a PR because I removed

// ->setTimezone($this->option('timezone', config('app.timezone'))) // removed

in ScheduleListCommand.php:42 and now when I run the command:

$ php artisan schedule:list

I've the attended result:

+---------+-----------+-------------+----------------------------+
| Command | Interval  | Description | Next Due                   |
+---------+-----------+-------------+----------------------------+
|         | 0 0 * * * |             | 2022-01-13 00:00:00 +00:00 |
+---------+-----------+-------------+----------------------------+
1 like
tykus's avatar

@ilex01 your problem was not providing the timezone option; the $this->option method does not provide for a fallback, it should have been:

->setTimezone($this->option('timezone') ?? config('app.timezone'))
Sinnbeck's avatar

@ilex01 don't change files in the vendor directory. And as you are the only one experiencing this, I doubt it's a general thing.

I suggest creating a new thread to get help with how to get your environment working

This thread was about why using a blade files as a cron job wasn't working. Short version. Laravel only works if you bootstrap it first.. And calling a random blade file won't bootstrap it

microscan5ep's avatar

@sinnbeck ilex01 is not the only one experiencing this error. I just ran "composer update" on one of my Laravel projects and I am getting the exact same error when running "php artisan schedule:list"

microscan5ep's avatar

When I say the exact same error, I am referring to "DateTime::setTimezone() expects parameter 1 to be DateTimeZone, string given"

Sinnbeck's avatar

@microscan5ep ok. It's just that I have never seen that error before. And I cannot find any threads online about it

I also made a brand new project using laravel new and that worked as expected

Maybe make a brand new thread with this specific issue as I suggested earlier, so we can dig into it

Please or to participate in this conversation.