Search code examples
phplaravelmodelcontrollernamespaces

Laravel upgrade broke model paths


I've performed a long-overdue update on a Laravel project from v5.7 to v9 and ran into a Target class does not exist error. I found this guide and used the first method to resolve the error (adding the namespace into the RoutesServiceProvider.php boot function). This resolved that error but now, everything is giving me Class "App\Whatever" not found.

I did notice that models are now stored in a Models directory within the app directory rather than directly in app, so have moved them to Models. I figured that might be breaking my use App\Whatever; lines at the top of my controllers, so I've tried use App\Models\Whatever and also use app\Models\Whatever (since the "a" is lowercase in the directory name) but no effect.

I should note I don't really have a good grasp of namespaces, MVC frameworks etc. so ELI5 etc :-)

Some of my controller:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Thing;
use App\AnotherThing;
...
    public function thing_summary($id) // show the summary view of the thing

    {

        if(Auth::check()) {

            $thing = Thing::find($id);
...

Solution

  • Laravel 7/8/9 sticks with strict namespacing. When you move the models to a new directory, you need to update the namespace in the models themselves (if specified at all) and any file that has a use for the model. If the models move from app/ to app/models, the namespace must be changed to App/Models