I have the following code in an api endpoint, which checks there are no relationships on a model to be deleted (and returns true to show it's in use):
CONTROLLER
public function destroy(Group $group)
{
if ($group->inUse()) {
return response(
['message' => 'This group cannot be deleted because it has either associated
catalogue items, users or organisations'],
409
);
}
$group->delete();
}
MODEL
public function inUse()
{
$models = [
'categories',
'items',
'organisations',
];
foreach ($models as $model) {
if (count($this->{$model}) > 0 ){
return true;
}
}
return false;
}
The line which I don't fully understand is where we check the number of relations for each model: count($this->{$model})
I read on php.net that $this->{$var} is a variable variable, but that can't be the case here, the first run through of the loop would return an undefined variable $categories:
if($this->$categories) {
//
}
Is this a laravel feature or special syntax? I did a quick search but nothing came up I could see.
Thanks in advance.