Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 485
Lovelock Avatar asked Nov 23 '16 12:11

Lovelock


1 Answers

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.

like image 179
aullah Avatar answered Sep 23 '22 15:09

aullah