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

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.

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.

You need to change User model namespace:
namespace App\Models;

Try renaming the namespace. :)
namespace App\Models;

change namespace, and use like this
namespace App\Models;

Related

Changing the path of models into a models directory

What I've done
I used the command php artisan make:model User without mentioning model folder as the command should have been php artisan make:model Models\User.
I am an amateur in terms of laravel and just started php and laravel last month and made a whole multi vendor project around it without the models being in models folder.
What I want
Now, I'm just trying to structure my code in a cleaner manner and facing errors as have to change the path of models wherever it is mentioned. I tried doing them but still can't get rid of the errors.
Latest version of Laravel, by default, file is already created inside the Models folder. If you have a model outside folder and what you want is to incorporate it there, the best thing to do is to change the built-in namespace at the top of everything. For example:
namespace App\Models;
If you want is to completely change the path, first create the model and then move folder manually. Finally, in namespace mention it:
namespace FolderA\FolderB\FolderC;.
Although it is always better to follow the default Laravel structure, is cleaner.
You need to modify your
Project/config/auth.php
file on
'model'=> AppName\Models\User::class,
The best thing to do is to move 1 model at a time and replace all of its import namespaces. Test your application if everything works and then continue with the next model and so on.
Example:
Move User.php from App\ to App\Models folder
Update the namespace of User.php from namespace App; to namespace App\Models;
Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
Check your application and run tests if you have them.
If everything is ok, you can continue moving the next model to the Model/ folder and repeat step 1 to 5
For the model User.php: As #saeed mentioned, update your config/auth.php if needed.
Note that IDE's these days can do the most work for you, like PhpStorm can do all the find & replaces for you when you pressing F6 on the User.php file in the tree and update its path.

Laravel package development - Target class [ControllerName] does not exist

I'm following a tutorial on how to create laravel packages.
I'm using Laravel v8.42.1 (PHP v7.4.3) and jetstream package.
I'm stuck on creating a controller for my package, I always get following error when trying to connect to my url via the laraval app (<base-url/playground):
Target class [TestVendor\TestPackage\Http\Controllers\PlaygroundController] does not exist.
The TestVendor\TestPackage\src\routes.php is recognized by the main application:
use TestVendor\TestPackage\Http\Controllers\PlaygroundController;
use Illuminate\Support\Facades\Route;
Route::get('/playground', [PlaygroundController::class, 'index']);
And is loaded from my ServiceProvider class:
$this->loadViewsFrom(__DIR__.'/resources/views', 'playground');
loadRoutesFrom(__DIR__.'/routes.php');
My namespacing is also normally correctly written in the composer.json of my package:
"autoload": {
"psr-4": {
"TestVendor\\TestPackage\\": "src/"
}
},
And I have my PlaygroundController in src/Http/Controllers/PlaygroundController.php:
namespace TestVendor\TestPackage\Http\Controllers;
use Illuminate\Routing\Controller;
class PlaygroundController extends Controller
{
public function index()
{
return view('playground::hello');
}
}
The view is also in the right package.
I'm using https://github.com/Jeroen-G/laravel-packager to scaffold my packages.
Did multiple composer auto-load and composer updates.
It seems my controller is not recognized in the main application, I think somewhere my name spacing is not correct?
I already had a look at:
Target class controller does not exist - Laravel 8
Solution did not work
Target class does not exist. problem in laravel 8
Solution did not work
You have namespaced your controller as:
namespace TestVendor\TestPackage\Http\Controllers;
In the line above though you say:
And I have my PlaygroundController in src/Http/PlaygroundController.php:
Unless that is a typo, you need to add a Controllers directory underneath Http and put your PlaygroundController in there:
src/Http/Controllers/PlaygroundController.php
For psr-4 autoloading, your folder structure and namespaces should mimic each other.

Yii2 Console: app\models Class not found but common\models found

I have an Yii2 web application with 50+ model class files that are located in /models directory.
Now I want to run some console scripts from /console/controllers/MyController.php using these models but get class app\models\ModelName not found error, despite of use app\models\ModelName at the top.
If I copy a model file to /console/models/ModelName.php or /common/models/ModelName.php (and make change in use) it works alright. Is there any option to use models from /model or should I refactor the application so that both web and console use model files from /common/models
If you are using yii2-advanced build take in mind that #app alias is set each time depending on what part of application you are using.
If you are making a call from frontend, #app will be equal to /path/to/project-root/frontend.
If from backend - /path/to/project-root/backend.
console - /path/to/project-root/console
You may add custom alias in /common/config/bootstrap.php, to make your classes available from root.
For example try to add Yii::setAlias('#root', dirname(dirname(__DIR__))); to /common/config/bootstrap.php and set namespace to root/models
Note: if you will try add #app to bootstrap.php, it will be automatically reassigned by framework.
Note 2: You may check how yii2 autoloader works in BaseYii.php

Laravel namespace issue after adding a folder under Models

I have 2 models in Laravel 5.5 under the "Models" folder. I then created a folder called "Internal" inside the Models folder and inserted my models in that folder. I updated all the namespaces and also did composer dump-autoload. But I now get an error that states that
"Class 'App\Models\Internal\LeaveApplication' not found".
Here is the Controller namespace:
use App\Models\Internal\LeaveApplication;
use App\Models\Internal\LeaveType;
Here is the model's namespace:
namespace App\Models\Internal;
use Illuminate\Database\Eloquent\Model;

In laravel, how to use sentinel in a controller?

I am using sentinel to manage my roles an permissions but in this project the permissions can be edited by the admin. so I am using a controller to assign the new permissions. however I cant use the Sentinel class in the controller archive I get the error:
Class 'App\Http\Controllers\Sentinel' not found
because of the namespace
my question is do I need to require with require_once all the Cartalyst files or is there another way?
You can use this namespace to solve your problem
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;

Categories