Support the ongoing development of Laravel.io →
Eloquent Architecture

Is there any benefit to the following 2 pieces of code in a model class? Which one should I adopt and why?

public function emails()
{
    return $this->hasMany('App\Email');
}

vs

public function emails()
{
    return $this->hasMany(App\Email::class);
}
Last updated 3 years ago.
0

This works since PHP5.5 it returnes a String with the class name and its fully qualified namespace. The only benefit I know is thats it's better for Typehinting in the IDE.

0

There are more benefits than just Typehinting:

  • You can easily inspect the class header and identify the class relationships it uses.
  • You get to know more easily, where a specific class is used, when you search for "Find usages in File" in your IDE.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

wyred wyred Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.