Laravel 5 - Moved User model to App/Models causing autoload issues
Asked Answered
C

4

7

Working on a inherited Laravel Spark project that contained two User models.

One is the standard Spark model inside the App directory but the other is inside App/Models. I have combined the two models and updated the auth.php to reference the User model inside the Models directory but composer dump-autoload is saying it cannot find the App/User model.

How can I tell the autoloader that the User model is not there any more but instead in the Models directory?

Edit:

I have changed the namespace to App/Models but still receive the error:

class_parents(): Class App\User does not exist and could not be loaded

In my terminal when running dump-autload

Second Edit:

Fixed, didn't realise the namespace was referenced so much. Did a find and replace on App\User and sorted the issue.

Carilyn answered 23/11, 2016 at 12:13 Comment(2)
Did you update the namespace in User.php?Revolt
Yes, have edited question :)Carilyn
B
3

A standard Laravel installation will work by simply changing the namespace as others have mentioned; however Laravel Spark references the User and Team models, therefore a namespace change alone will not work.

You shouldn't edit any files inside the vendor/laravel/spark-aurelius (aurelius codename will vary depending on your version) as these changes are not tracked.

With Spark, you should add the following lines into your app/Providers/SparkServiceProvider.php:

public function register()
{
    Spark::useUserModel('App\Models\User');
    Spark::useTeamModel('App\Models\Team');
}

You can set your own custom App\Models directory, rather than using the above example.

Finally you will need to update any references you make to your models, e.g. update controllers from use App\User to use App\Models\User.

Source: Laravel Spark 6.0 Customization

5th Jan 2020 Update: Remember to also update STRIPE_MODEL and BRAINTREE_MODEL values in your .env to your new namespace.

Laravel Spark 9.0 removes Braintree support, therefore you only need to update CASHIER_MODEL in 9.0.

21st Dec 2020 Update: Laravel 8.x now keeps all models in the app\Models directory by default. Even if you're on an older version of Laravel (e.g. 6.x) but are using Laravel Spark 11, then you do not need to do any of the above. Laravel Spark 11 will assume your models live in app\Models.

Balancer answered 2/9, 2018 at 19:38 Comment(1)
Note: It is also necessary to update the configuration in config/auth.php.Moorfowl
C
2

You need to change User model namespace:

namespace App\Models;
Coopersmith answered 23/11, 2016 at 12:15 Comment(1)
Yes should have mentioned. The namespace is correct. In terminal I get: class_parents(): Class App\User does not exist and could not be loadedCarilyn
S
2

change namespace, and use like this

namespace App\Models;
Subclinical answered 23/11, 2016 at 12:19 Comment(0)
M
2

Try renaming the namespace. :)

namespace App\Models;
Managerial answered 23/11, 2016 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.