Answer the question
In order to leave comments, you need to log in
How to pass method name to variable in Laravel?
I need to transfer the name of the method with brackets to a variable. And I don't know how to write it. Tried 'method()' and other password guessing - no luck.
Statuses in the Model
//Опубликован
public function active()
{
return 1;
}
//Не опубликован
public function inactive()
{
return 2;
}
//Проверка
public function isActive()
{
if ($this->status_id==$this->active()) return true;
return false;
}
//Обновление статуса
public function statusUpdate($status)
{
$this->update(['status_id'=>$this->$status]);
}
$product->statusUpdate('active()');
Answer the question
In order to leave comments, you need to log in
What kind of horror are you doing?
Okay, in the forehead it will be like this:
//Обновление статуса
public function statusUpdate($status)
{
$this->update(['status_id'=>$this->{$status}()]);
}
$product->statusUpdate('active');
class Foo
{
public const ACTIVE = 1;
public const INACTIVE = 2;
public function isActive()
{
return $this->status_id === static::ACTIVE;
}
public function statusUpdate($status)
{
$this->update(['status_id' => $status]);
}
}
(new Foo)->statusUpdate(Foo::ACTIVE);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question