Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sudhanshugargG's avatar

Laravel get the model name from relationship

I have a model App\Models\User in which I define a relationship child with model App\Models\User. Meaning another user can be a child of given user.

Now I want to retrieve the value of model-name refered to by relationship child. Answer: App\Models\User

how to do that in Laravel?

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

Try this for getting the full name with namespace.

$className = get_class($user->child()->getRelated());

Try this for getting the base name-

$baseClass = class_basename($className);
11 likes

Please or to participate in this conversation.