在 Laravel 工作中,我有:
use Spatie\Valuestore\Valuestore;
Run Code Online (Sandbox Code Playgroud)
和
public function __construct()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
Run Code Online (Sandbox Code Playgroud)
和
public function handle()
{
if($this->settings->get('foo') == 'test') {
etc...
Run Code Online (Sandbox Code Playgroud)
对此我得到一个错误Undefined property App\Jobs\MyJobName::$settings。出了什么问题?
即使我这样做:
public function handle()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
if($this->settings->get('foo') == 'test') {
etc...
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误。
根据评论更新
MyJobName在自定义 artisan 命令中调用,它碰巧也使用Valuestore,但我认为这是不相关的。
在课堂里CustomCommand:
use Spatie\Valuestore\Valuestore;
Run Code Online (Sandbox Code Playgroud)
和
public function __construct()
{
parent::__construct();
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
Run Code Online (Sandbox Code Playgroud)
和
public function handle()
{
if($this->settings->get('foo') == 'test') // This works in this custom command!
{
$controller = new MyController;
MyJobName::dispatch($controller);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,CustomCommand我的使用Valuestore方式与中的完全相同,MyJobName但在后者中它不起作用。根据其中一条评论:我不会$this->settings全局化,因为我在任何一个中都没有这样做CustomCommand,而且它在那里工作得很好。
更新2
如果我按照注释在函数$settings;上方添加 protected __construct(),它仍然不起作用,同样的错误。
只需在您的作业类中将设置属性声明为公共即可。
public $settings;
public function __construct()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
}
Run Code Online (Sandbox Code Playgroud)
Ara*_* G. -1
如果使用JOB by QUEUE,则需要所有的请求或者SQL查询都通过该方法来做handle
public function handle()
{
$this->settings = Valuestore::make(storage_path('app/settings.json'));
....
}
Run Code Online (Sandbox Code Playgroud)
因为构造函数在创建类的对象时起作用,并且该对象被序列化并存储在数据库中,并且在反序列化和触发句柄之后。
| 归档时间: |
|
| 查看次数: |
4945 次 |
| 最近记录: |