Laravel namespace issue after adding a folder under Models - php

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;

Related

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.

PhpStorm does not recognize my models in the app folder

I am using Laravel 7. I use PhpStorm, and great fan of it.
I want to store all of my model files in a single folder named Models into app folder. I changed this line:
namespace App; -> namespace App/Models.
But PhpStorm is giving me an error. It doesn't recognize my folder:
Undefined constant 'Models'
How can I fix this?
Use the anti slash for namespace
namespace App\Models;

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.
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;

Can't find a particular model if its not in \App

So I noticed something very strange when I deployed my project on a server (It works on local server) , one of my models doesn't work in "models" directory I've created, it only works when I put the model.php in \App and it has the namespace as the other models , how come?
namespace App\Http\Models;
Error: Class 'App\http\Models\Themodel' not found
If your models folder is in App\Models then the namespace should be:
namespace App\Models;
In your controller import the model in your controller like this:
use App\Models\Themodel;

Extend Laravel 5 Response Facade

I am getting a namespacing issue when trying to extend the Response facade in Laravel 5. I have created a new folder tree under the app directory called Extensions\Facades. In this folder I have a file called AjaxResponse.php which has the following contents:
<?php namespace App\Extensions\Facades;
use Illuminate\Support\Facades\Response;
class AjaxResponse extends Response{
public static function send($code,$body,$http_code=200){
parent::json( array(
'status'=>(string)$code,
'body' =>$body
) )->setStatusCode($http_code)->send();
exit();
}
}
I am registering this as a service provider in config/app.php like I understand I am supposed to:
providers=[
//..normal stuff
'App\Extensions\Facades\AjaxResponse',
]
And this is throwing the normal namespace error of class not found:
FatalErrorException in ProviderRepository.php line 150:
Class 'App\Extensions\Facades\AjaxResponse' not found
Can anyone shed any light on why the class is not found?
Go to project root folder and in the terminal type
composer dump-autoload
Everything should be fine then. When you create a new folder, composer does not know about it, so it can not autoload files from it, even if they are psr-4 namespaced.
EDIT Also you need to declare alias for your facade in config/app.php under aliases array, not the providers one:
'AjaxResponse' => 'App\Extensions\Facades\AjaxResponse',

Categories