Simo A.
Simo A.

Reputation: 1360

"User" model broken after Laravel 4.1 upgrade

I have upgraded Laravel from 4.0 to 4.1 recently in a few instances. Today I did the upgrade in yet another instance and found out that there is something wrong with the User model. The models/User.php file is still there, but I don't think it is used by Laravel anymore. My question is: why?

To demonstrate the issue, I have created the following entries in my routes.php:

Route::get('test1', function()
{
  $course = Course::find(4);
  return ($course->users()->first());
});

Route::get('test2', function()
{
  $user = User::find(22);
  return ($user->courses()->first());
});

Both these entries are correct regarding syntax and the database object (course with id 4 exists and user with id 22 exists). My Course.php model has the following:

public function users()
{
  return $this->belongsToMany('User')->withPivot('participant_role')->withTimestamps();
}

And my User.php has a corresponding entry:

public function courses()
{
  return $this->belongsToMany('Course')->withPivot('participant_role')->withTimestamps();
}

Now if I launch the first URL, /test1, I get a working JSON entry as a result. As expected.

With the second URL, /test2 however I get an error message:

BadMethodCallException

Call to undefined method Illuminate\Database\Query\Builder::courses()

open: /home/simoa/laravelapps/clientname/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php `

I think there is something wrong here. Why is my Laravel instance trying to call the courses() method from the Illuminate\Database\Query\Builder class? That isn't normal, right?

As I said earlier, everything else works perfectly, except things to do with the User model.

Upvotes: 0

Views: 397

Answers (2)

Simo A.
Simo A.

Reputation: 1360

The issue was caused by an invalid entry in the file vendor/composer/autoload_classmap.php.

For some reason during the 4.1 upgrade (probably running the command: composer update) the entry for 'User' => $baseDir . '/app/models/User.php' has turned into 'User' => $baseDir . '/app/models/old_User.php' in that file.

I did have an old backup/dev file called old_User.phpin my models directory and for some reason, it seems, composer has mapped User class to that file.

Solution: delete old_User.php and re-run composer update.

Upvotes: 2

cecilozaur
cecilozaur

Reputation: 705

try running php artisan clear-compiled and then php artisan optimize

Upvotes: 1

Related Questions