I am using a construct to get request variables only once, since I need to use them in more than one method, I do not want to repeat code.
public function __construct(Request $request)
{
$this->labelId = $request->label;
$this->statusId = $request->status;
$this->monthId = $request->month;
}
This works for my "standard" view, but not for the "label view":
public function standard()
{
$appointments = Appointment::latest('start')->status($this->statusId)->label($this->labelId)->month($this->monthId)->get();
return view('appointments.index', compact('appointments'));
}
public function label(Request $request)
{
$appointments = Label::with(['appointments' => function ($query) use ($this->labelId, $this->statusId, $this->monthId) {
$query->label($this->labelId)->status($this->statusId)->month($this->monthId)->get();
}])->get();
return view('appointments.label', compact('appointments'));
}
I am getting the error:
Cannot use $this as lexical variable
And the issue is here:
function ($query) use ($this->labelId, $this->statusId, $this->monthId)
My question is this, can I somehow use the variables anyway in the label method using the construct, or is there maybe a better way to accomplish this?