Skip to content

Instantly share code, notes, and snippets.

@AlphaRomeoMike
Created March 8, 2022 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlphaRomeoMike/4f2e0978db36bdd18583e0044f399caa to your computer and use it in GitHub Desktop.
Save AlphaRomeoMike/4f2e0978db36bdd18583e0044f399caa to your computer and use it in GitHub Desktop.
Laravel Cron Custom Artisan Command
<?php
namespace App\Console\Commands;
use App\Mail\BillGenerated;
use App\Models\Category;
use App\Models\SubCategory;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class GenerateBill extends Command
{
/**
* The name and signature of the console command.
* Modify the signature as requried
*
* @var string
*/
protected $signature = 'generate:bill';
/**
* The console command description.
* Modify to show when the -h or --help flag is executed with the command
* @var string
*/
protected $description = 'Generate bills for users';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$users = User::all()->where('role', 'USER');
foreach ($users as $user) {
$user->bills()->create([
'name' => 'Bill for ' . $user->name,
'amount' => rand(100, 1000),
'paid' => rand(0, 1),
'category_id' => Category::all()->random()->id,
'subcategory_id' => SubCategory::all()->random()->id,
'user_id' => $user->id,
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment