How to call a variable in models in laravel?

Member

by larissa , in category: PHP Frameworks , 4 months ago

How to call a variable in models in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 4 months ago

@larissa 

In Laravel, you can access a variable in a model by using the $this keyword.


For example, if you have a variable named $name in your model, you can access it like this:

1
$this->name;


You can also use the magic __get() method to access the variable directly like this:

1
$name = $this->name;


Make sure that the variable you are trying to access is defined in the model class.