0

What means $this in laravel?

For example:

public function car() {
  return $this->hasOne('App\Car');
}

Can I use this for example too?

public function car() {
  return $car->hasOne('App\Car');
}
codedge
  • 4,754
  • 2
  • 22
  • 38
  • 6
    Does this answer your question? [What does the variable $this mean in PHP?](https://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php) – codedge May 17 '20 at 21:41
  • Welcome to StackOverflow. In this particular case `$this` refers to a Laravel Eloquent model where the `car()` method is located in. `$car→hasOne` cannot be used, because `$car` is undefined variable. Please refer to a basic OOP example stated in the comment above. `$this` is nothing more than just a reference to the class. – Serhii Matrunchyk May 17 '20 at 22:14

1 Answers1

1

It's not a laravel feature, but a PHP feature. Please read the following document here.

Simply put, $this refers to the current instance of the object during runtime.

D. Foley
  • 1,034
  • 1
  • 9
  • 23