Created
March 8, 2022 09:07
-
-
Save AlphaRomeoMike/4f2e0978db36bdd18583e0044f399caa to your computer and use it in GitHub Desktop.
Laravel Cron Custom Artisan Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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