1

In my Laravel Scheduler

protected function schedule(Schedule $schedule)
{

    if(env('APP_DEBUG') == true){
        $cronInterval = 'everyMinute()';
    } else {
        $cronInterval = 'daily()';
    }


    $schedule->command('inspire')
            ->$cronInterval
            ->emailOutputTo(env('MAIL_TO')); // tried 

How do I dynamically chain a function in Laravel ?

enter image description here

2
  • Please suggest a sample code base on your comment, I will try now. Commented Dec 3, 2020 at 16:46
  • 1
    Notice how it's saying Undefined property? It's thinking that everyMinute() and daily() are properties, not methods. I think the answer below, omitting the () from the string and appending like ->{$cronInterval}() should solve your issue :) Commented Dec 3, 2020 at 16:53

1 Answer 1

3

It's called variable functions. Try this:

$cronInterval = env('APP_DEBUG') ? 'everyMinute' : 'daily';

$schedule->command('inspire')
    ->{$cronInterval}()
    ->emailOutputTo(env('MAIL_TO'));
Sign up to request clarification or add additional context in comments.

Comments

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.