首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有特点的Laravel控制台命令

有特点的Laravel控制台命令
EN

Stack Overflow用户
提问于 2019-06-07 15:04:23
回答 1查看 1.5K关注 0票数 1

我为我正在研究的一个特性设置了一些Config变量,其中之一是一个data_format变量,它要么是json,要么是xml。

它是这样设置的:

代码语言:javascript
复制
$this->data_format = config('rightmove.FORMAT');

在Controller中,我可以运行dd($this->data_format)并返回:"json

我已经让这是一个控制台命令,但是当我尝试使用dd的$this->data_format运行控制台命令时,它会返回

以及随后我的其他代码错误。

我运行了一个php artisan config:cache命令来清除缓存,但是控制台似乎没有选择这个配置变量?

我的App\Console\Commands\UpdateRightMove看起来如下:

代码语言:javascript
复制
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Traits\RightMoveTrait;
use App\Property;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use QCod\AppSettings\Setting\AppSettings;


class UpdateRightmove extends Command
{
    use RightMoveTrait;

    private $is_set;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rightmove:update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Updates Rightmove - Creates / Updates and Deletes properties from the Rightmove API';

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

        $this->is_set = setting('rightmove');
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        if($this->is_set == 1)
        {
            // Rightmove is Enabled via settings (Run Command)...
            $now = Carbon::now();

            // Check App Cache for Last Time RightMove Artisan Command was Ran...
            //$last_ran = cache('rightmove_update');
            $last_ran = '2019-06-07 14:52:40';

            if($last_ran == NULL)
            {
                // Send ALL Properties to Rightmove, First Time Ran....
                $properties = Property::whereNotNull('ref')
                    ->where('beds', '>', 0 )
                    ->where('price', '>', 0)
                    ->where('name', '!=', '')
                    ->where('city', '!=', '')
                    ->whereRaw('LENGTH(postcode) >= 5')
                    ->get();
            }
            else
            {
                $from_date = Carbon::now();

                // Get a Difference between last ran and now...
                $time_diff = $from_date->diffInHours($last_ran);

                // This will run and get properties that have changed since the last task ran
                $to_date = Carbon::now()->subHours($time_diff);

                // Get Filtered Properties & One's added / updated in last 12 hours...
                $properties = Property::whereNotNull('ref')
                    ->where('beds', '>', 0 )
                    ->where('price', '>', 0)
                    ->where('name', '!=', '')
                    ->where('city', '!=', '')
                    ->whereRaw('LENGTH(postcode) >= 5')
                    ->whereBetween('created_at', [$to_date, $from_date])
                    ->orWhereBetween('updated_at', [$to_date, $from_date])
                    ->get();
            }

            // Get Settings....
            if(setting('overseas') == 0)
            {
                // UK Based....
                $this->update_feed_uk($properties);
            }
            else
            {
                // Overseas....
            }

            Cache::rememberForever('rightmove_update', function()
            {
                return now()->toDateTimeString();
            });
        }
    }
}

我的RightMoveTrait如下:

代码语言:javascript
复制
<?php

namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Property;
use App\PropertyType;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

trait RightMoveTrait
{
    private $network_id;
    private $branch_id;
    private $data_format;
    private $environment;
    private $allow_delete;
    private $current_time;

    public function __construct()
    {
        $this->network_id = config('rightmove.NETWORK_ID');
        $this->branch_id = config('rightmove.BRANCH_ID');
        $this->data_format = config('rightmove.FORMAT');
        $this->allow_delete = config('rightmove.ALLOW_DELETE');
        $this->environment = config('rightmove.ENVIRONMENT');

        $now = Carbon::now();
        $this->current_time = $now;

        $this->refs = [];
    }

    function update_feed_uk($properties)
    {
        dd($this->data_format);
    }
}

编辑看起来控制台运行正在跳过特征中的__construct。因此,在Console命令(UpdateRightMove)中的构造方法中,我添加了变量,并且没有问题。

代码语言:javascript
复制
 public function __construct()
    {
        parent::__construct();

        $this->is_set = setting('rightmove');

        $this->network_id = config('rightmove.NETWORK_ID');
        $this->branch_id = config('rightmove.BRANCH_ID');
        $this->data_format = config('rightmove.FORMAT');
        $this->allow_delete = config('rightmove.ALLOW_DELETE');
        $this->environment = config('rightmove.ENVIRONMENT');

        $now = Carbon::now();
        $this->current_time = $now;

        $this->refs = [];
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-10 09:29:05

您的特性的__construct()函数不被调用,因为该类使用自己的__construct()函数。

在您的UpdateRightmove类中:

代码语言:javascript
复制
use RightMoveTrait {
    RightMoveTrait::__construct as private _rightMoveTraitConstruct;
}

public function __construct()
{
    parent::__construct();
    $this->_rightMoveTraitConstruct();

    $this->is_set = setting('rightmove');
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56496874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档