Home > Enterprise >  Laravel8: Class not found despite defining it in the models
Laravel8: Class not found despite defining it in the models

Time:01-25

I used the model "clients.php" but when I load in the ClientsController

$clients= Client::all()->sortby('name');

I get the: Class "App\Http\Controllers\Admin\Client" not found

What would be the solution? I knew that if I create a "clients" model, then it should load as "client" in my function

CodePudding user response:

You should use full namespace, for example (if Client model is stored inside Models directory):

$clients = \App\Models\Client::all()->sortby('name');

or you can add use App\Models\Client; at the beginning of the file after the namespace declaration and then just use

$clients = Client::all()->sortby('name');

CodePudding user response:

You have to import the Client model. If you use Laravel 8 then :

use App\Models\Client;

$clients = Client::all()->sortby('name');

OR

If you use Laravel 7 or below then:

use App\Client;

$clients = Client::all()->sortby('name');

CodePudding user response:

you can do . use your client controller

use App\Models\Client;

then

$clients= Client::all()->sortBy('name');

OR

call direct model in your controller.

$clients = \App\Models\Client::all()->sortBy('name');

CodePudding user response:

If you provided the namespace in the Controller and it's still not working, open the terminal and type "composer dump-autoload". I do it every time i have a new Model.

  •  Tags:  
  • Related