3

I've a website made in Laravel 4.2 and we have a monthly membership with 30 credits. Even if user doesn't use all of them, they should expire at valid_till date.

I wish to have a piece of code which will run at 12 am every night and will set credit as 0 in everyone's account who's valid_till date is equal to yesterday or before that.

If there could be something like task scheduling in laravel 5.1, which runs a function on particular time of day daily, that would be perfect.
I know cron can be set up in Laravel 4.2, using commands but I'm unable to understand how to use it in my case?

0

1 Answer 1

4

Create a file like the following in app/commands, replace my comment in the fire method with your update function.

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class setCreditToZero extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'set:zero';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        // your update function here
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }

}

Register your command in app/start/artisan.php

Artisan::add(new setCreditToZero);

Now set a cronjob using command on your terminal

crontab -e

And in this file set a cronjob with this line

0 0 * * * /usr/bin/php /path/to/laravel/baseDir/artisan set:zero

And you should be good to go

Sign up to request clarification or add additional context in comments.

10 Comments

Sir I'm unable to find Console folder in Laravel 4. I think it come after Laravel 5. I've put this file in app/commands. I've registered the command in app/start/artisan.php as Artisan::add(new setCreditToZero); I've put this cronjob line using command crontab -e Just guide me if I've set cronjob correctly. I'm unable to judge if whole system had been set up correctly or not.
Thank you for your answer, along with Laravel documentation I'm able to figure out the problem, but your answer is more of Laravel 5 answer.
No, the command structure is changed in L5, this one should work for 4
There's nothing like App\Console in L5. So registering the command in Kernel.php is not possible in L4, it's done in artisan.php in L4, apart from that app\commands is the place where file is stored not app\console\commands. Please also mention where to put this cronjob line in your answer, I'm able to figure it out but it took me a couple of more searches for it.
@baao, which file are you referring to by And in this file set a cronjob with this line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.