I've previously manually deleted some models files in App* and created other with just removing the "s" symbol as because laravel by default can recognise the models from there name , and models by Laravel standadrd must be written normal not in plural.
Before deleting the models that i created them using php artisan make:model Buildings -m
The new model i created after i removed the (buildings) model is php artisan make:model Building Notice that i just created a new model without 's'
Now in my User model i have created method :
public function UserAssignedBuilding(){
return $this->hasManyThrough('App\Building','App\Area','user_id','area_id');
}
Building.php Model file
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Building extends Model
{
protected $table = 'buildings';
public function areas(){
$this->belongsTo('App\Area');
}
}
Area.php Model file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Area extends Model
{
protected $fillable = [
'name'
];
public function users(){
return $this->belongsToMany('App\User','area_user','area_id','user_id');
}
public function buildings(){
return $this->hasMany('App\Building');
}
}
In php artisan when i run the following command, to get the user assigned buildings:
>>> User::find(4)->UserAssignedBuilding
PHP warning: include(C:\xampp\htdocs\hse\vendor\composer/../../app/Buildings.php): failed to open stream: No such file or directory in C:\xampp\htdocs\hse\vendor\composer\ClassLoader.php on line 444
It seems that the error is the framework trying to load the (Buildings.php) model file, which i had already delete it and created (Building.php) instead .
I run the following :
C:\xampp\htdocs\hse>composer dumpautoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Package manifest generated successfully.
But issue not fixed
Also tried to get buildings rows, other error showing:-
>>> Building::all()
PHP Fatal error: Class 'Building' not found in eval()'d code on line 1
In your command you are missing model namespace
It should be
App\User::find(4)->UserAssignedBuilding
And
App\Building::all()
Also change method UserAssignedBuilding
to buildings
The error is showing in Tinker (php artisan tinker) , When i dumped the composer autoload file
composer dump-autoload
Then tried again in tinker , the same error is showing like the composer dump command did not do it work, but when i close the current tinker and open new one (php artisan tinker) , every thing now work perfectly .
So , the solution in brief is when you run the dumpautoload command, you need to restart the tinker to see the changes.
I faced the same issue but in my case the problem was case sensitivity of composer autoload.
I had created user
class (with small 'u') using composer artisan make
. So it created user.php
file for me. Afterward, I decided I like User
(with capital 'U') more, so I changed the class name but not the containing PHP file.
So the problem was composer was looking for User.php
file instead of user.php
. changing the containing file to User.php
did the job for me.