Передача переменной в макрос Illuminate Query Builder - PullRequest
0 голосов
/ 12 сентября 2018

Мне нужно передать переменную моему экземпляру Builder, желательно не передавая ее через параметр функции при каждом вызове. Есть ли хороший способ сохранить переменную в экземпляре Builder или какой-то другой способ сделать это?

Builder::macro('with', function (...$tables) {
    $period = ???; // How do I set this for every call to "with()"?

    $joins = collect($this->joins)->pluck('table');

    foreach ($tables as $table) {
        $tableName = $period.'_'.$table;
        if (!$joins->contains($tableName))
            $this->leftJoin($tableName, $tableName.'.symbol', '=', 'profile.symbol');
    }

    return $this;
});

class List extends BaseList
{
    // The current period
    protected $period;

    public function __construct($period)
    {
        $this->period = $period;
    }

    protected function base()
    {
        return DB::table('profile')->select('profile.symbol')
            ->with('technicals') // This is the macro that needs $this->period.
            ->limit(100);
    }
}
...