<?php Schema::create('cronjob', function($table) { $table->increments('id'); $table->string('type', 40); $table->text('data')->nullable(); $table->integer('user_id')->unsigned()->default(0); // user who created the cron $table->datetime('executed_at')->nullable(); // the execution start time $table->datetime('completed_at')->nullable(); // the complete timestamp });
Don’t forget also create a model class, Cronjob.php
$path = storage_path('files/users/' . \Auth::user()->id . '/html'); @mkdir($path, 0755, true); $filename = 'awesome-html-file.html'; file_put_contents($path . '/' . $filename, $html); // save to a temporary html file
// schedule generate pdf in cronjob $cronjob = newCronjob(); $cronjob->type = 'generate_pdf'; $cronjob->data = json_encode([ 'html' => $filename, ]); $cronjob->user_id = \Auth::user()->id; // keep the current user identity, because in cron, it won't know which user is current user $cronjob->save();
returnredirect()->back() ->with('alert.success', 'PDF is currently processing in background. Please refresh the page later.'); }