So basically I am trying to pass current object variable to my anonymous function like this:
$options = Option::whereHas('texts', function($query) use ($this->language) {
$query->where(['language_id' => $this->language->id, ]);
})->where(['active' => 1, ])
->get();
But it gives:
syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')'`.
Anyway, if I will set another variable to $this->language and pass it in anonymous function, it will work properly.
$language = $this->language;
$options = Option::whereHas('texts', function($query) use ($language) {
$query->where(['language_id' => $language->id, ]);
})->where(['active' => 1, ])
->get();
So, what is correct way to pass object variable to "use" section in anonymous function?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire