How to run task scheduler locally on Laravel 7? - laravel

I want to learn how to use task scheduler in Laravel 7
According to the documentation, I created a task that should send out a test message to the mail on Mondays at 16:00. Judging by the documentation for local development, there is no need to add a cron entry. I just run the command php artisan schedule:work get this error Command "schedule:work" is not defined. Where am I making a mistake?
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$test = 'test';
$email = Emails::where('id', 1)->first();
Mail::to($email)->send(new TestMail($test));
})->mondays()->at('16:00');
}
After I tried to add the line * * * * * cd /public_html/my_project && php artisan schedule:run >> /dev/null 2>&1 but got the following message bash: README_CI.md: command not found

you can try this
protected function schedule(Schedule $schedule)
{
$schedule->command('demo:cron')
->everyMinute();
}

you have cd /your/path. better do php /public_html/my_project/artisan schedule:run as cd'ing in cronjob does not work unless you have a bash script instead of inline.
as for schedule:work, that commmand should exist, does artisan (without arguments produce a list of options including schedule:work?

Related

Laravel: How to add signature option to migrate:rollback command?

I have multiple databases and the migrations for each database are stored in a different folder. Hence, I want to override the migrate:rollback command and add an option for a folder instead of specifying the path every time.
So instead of running the following command:
php artisan migrate:rollback --path=/database/migrations/{{folder}}
I want to run:
php artisan migrate:rollback {{folder}}
How can I achieve this?
protected $signature = 'test {folder}';
protected $description = 'Testing Purpose';
public function handle()
{
$this->ask('Enter Folder Name?');
$folder = $this->argument('folder');
$command = "migrate:rollback --path=/database/migrations/{$folder}";
Artisan::call($command);
}

Laravel: how to simplify artisan commands?

I want to simplify the following artisan commands because I have several databases and the migrations for each database are stored in a separate folder.
php artisan make:migration {{name}} --path=/database/migrations/{{folder}}
php artisan migrate:rollback --path=/database/migrations/{{folder}}
to
php artisan make:migration {{name}} {{folder}}
php artisan migrate:rollback {{folder}}
Is this possible and if so how can I implement it?
Since this is not an option in the Laravel commands, a way to implement this yourself is by writing you own commands that call other artisan commands.
To do so, in your terminal write, for example, php artisan make:command MigrateWithPath to create a new command at app/Console/Commands/MigrateWithPath.php. Then, you can call the vanilla implementation Laravel provides at (vendor\laravel\framework\src) \Illuminate\Database\Console\Migrations\MigrateMakeCommand but then in a way that you specify.
Be sure though that the name of your new command needs to be different from the Laravel one, to prevent recursions. Therefore, I have prefixed the name with app: to be like app:make:migration, but feel free to use something else.
Take a look at the following suggestion:
class MigrateWithPath extends BaseCommand
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'app:make:migration {name : The name of the migration}
{folder? : The location where the migration file should be created}';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$this->call('make:migration', [
'name' => $this->argument('name'),
'--path' => '/database/migrations/' . $this->argument('folder'),
]);
return 0;
}
Then do the same for the rollback command.

laravel - 'Auth' is not working in cron schedule

I have created a cron job in the 'commands' files. and I want to get the id of the user using Auth::user()->id but I am getting the error. Where am I missing?
My error:
Trying to get property 'id' of non-object
My controller
use Illuminate\Support\Facades\Auth;
protected $signature = 'orders:minute';
public function handle()
{
$id = Auth::user()->id;
}
Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('orders:minute')
->everyMinute();
}
You can't do that because a user doesn't run the scheduled command, so Auth::user() object will always be null.
To fix this, you can save User Id in Database and fetch the data on scheduled command execution.
You can't do that because a user doesn't run the cron scheduled.
For the use case you're showing you'd have to provide it yourself, for example as a command argument:
protected $signature = 'command:Orders {user_id : id of the recipient model}';
public function handle() {
$user_id = $this->argument('user_id');
$user = User::findOrFail($user_id);
// other commands
}
So executing php artisan command:SendNotification 1 would act as if user 1 was authenticated.
Cron means to run something automatically. You can't do that because a user doesn't run the cron scheduled. You only get Auth::user()->id when the user login

Can't run created command on Laravel Scheduler locally

I'm trying to run created command locally, but when hitting php artisan schedule:work it shows no sceduled commands are ready to run. What's wrong?
My Kernel:
protected $commands = [
'App\Console\Commands\AddEntry',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('add:entry')->everyMinute();
}
I also edited AppServiceProvider
public function boot()
{
//
Schema::defaultStringLength(191);
}
Command itself is working and it's visible on php artisan list

How can I run scheduler every one minute in Laravel without setting cron job?

I want to run the scheduler in every minute on the server without setting the cron job with ssh .Please let me know Is there any way to do this ?
Thanks
Assumption: you have set cron job to your project directory
Create a command using
php artisan make:command CommandName
add your logic under handle function
public function handle()
{
//add your logic here
}
register your command under Kernel.php under $commands array
protected $commands = [
Commands\Test::class,
];
schedule command under
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')->everyMinute();
}

Resources