You need to use it correctly. Currently its a string, so php has no way of knowing that you mean a class name.
use App\Models\MyModel;
public function MyModel()
{
return $this->hasOne(MyModel::class);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a relationship in this form:
public function MyModel()
{
return $this->hasOne('App\Models\MyModel');
}
But when I change it to:
use App\Models\MyModel;
public function MyModel()
{
return $this->hasOne('MyModel');
}
I get the error of Class Not Found about MyModel.
What could cause this? I did refactor a typo in the project (renamed the Models folder name from Model to Models if that matters)
@Ligonsker Did you wrap it in ' ' ? Can you show the code?
return $this->hasOne('MyModel::class'); // wrong
return $this->hasOne(MyModel::class); //correct
Please or to participate in this conversation.