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

Shivam Kesarwani's avatar

Code Navigation In Laravel For Model Attributes

Whenever we use Model static methods such as "Where", "find", etc. return instance is of mixed type, it makes method hinting and checking method usage from model becomes difficult in large projects.

example:

class School extends Model {

	public function students() 
	{

		return $this->hasMany(Students::class);

	} 
}
	$company = Company::find(1); // this returns mixed type instead of Company

	$students = $company->students();

navigating directly from ->students() is not possible for mixed type, also searching usage from model is not possible.

Any Smart solution for this problem.

0 likes
3 replies
neilstee's avatar

@shivam kesarwani

$company = Company::find(1); // this returns mixed type instead of Company

That is actually wrong, that find() method will surely return Company instance. Can you try to dd($company)?

Also, I'm a bit confused because in your code you have School model but you call Company instead, should it be:

class Company extends Model {

	public function students() 
	{

		return $this->hasMany(Students::class);
	} 
}

This should work properly if your database met the requirements for them to link (students table should have company_id column)

Shivam Kesarwani's avatar

@neilstee I agree on dd() we'll get Company Instance, but when you'll hover the cursor over $company variable it will show you mixed type, also you'll not get method suggestion as you get for stricted type hinted variable.

also if you do find all reference of student method in vs code, you won't find $company->students(); in the results

Please or to participate in this conversation.