【问题标题】:hi im a bit new into laravel and php i got this error [InvalidArgumentException]嗨,我对 laravel 和 php 有点陌生,我收到了这个错误 [InvalidArgumentException]
【发布时间】:2017-06-26 14:36:41
【问题描述】:

[InvalidArgumentException]无效的预定回调事件。必须是字符串或可调用的。 这是代码

protected function schedule(Schedule $schedule)
{
    // $schedule->command('inspire')
    //          ->hourly();
   $schedule->call($this->consult());
}

/**
 * Register the Closure based commands for the application.
 *
 * @return void
 */
protected function consult()
{//try {
    $url=DB::table('remote_services')->pluck('url');
   foreach ($url as $url){
       echo $url;
       echo  '   ';}
//}catch (InvalidArgumentException $e ){
  //  echo 'captured exception';
}

【问题讨论】:

  • 为您提供了错误的行号,以及堆栈跟踪。两者在帮助调试代码方面都非常有用。

标签: php database laravel laravel-5.4


【解决方案1】:

错误是因为你将错误类型的参数传递给call方法,例如,你有这个:

$schedule->call($this->consult());

在这里,您实际上调用了$this->consult() 方法并传递了结果;这相当于:

$methodCallResult = $this->consult();
$schedule->call($methodCallResult);

但是,这里的调用方法实际上接受CallableString。如果是String,则字符串可以是SomeClass@methodNameSomeClass::staticMethodName

在可调用的情况下,它可以是 Closure/Anonymous Function 或像 [$anObject, 'someMethod'] 这样的实例方法,在您的情况下,它可能是以下内容:

// Call the consult method of same/this class
$schedule->call([$this, 'consult']);

另外,在这种情况下,您的 consult 方法应该 (可能不确定,所以先尝试保护)public 而不是 protected

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多