Laravel 4: Model Class not found - php

I am getting a Class 'App\Models\User' not found error when I try too use the User class inside a controller class method. I have looked everywhere and tried everything with no luck! Here's what I've tried:
Check that class exists and is in the right path (it works everywhere else)
Add use App\Models\User; to the top of the controller file and just use User
Tried: new \App\Models\User
Run: composer dump-autoload
Run: php artisan dump-autoload
Run: php artisan clear-compiled
When I do dd(class_exists('App\Models\User')), I get \vendor\laravel\framework\src\Illuminate\Support\helpers.php:513:boolean false which confirms that the class really isn't accessible for some reason.
Any ideas?
EDIT
You will find questions similar to this but not the same. Please read question carefully. I didn't say the controller class was missing. I said a model class (User) was not accessible from inside a particular controller class. And that the model class works everywhere else.
EDIT: Code Excerpt
<?php
use App\Models\User;
use App\Models\Role;
use App\Models\Advert;
use App\Models\AdvertPhoto;
use App\Models\AdvertMetum;
use App\Models\AdvertMetaDatum;
use App\Models\AdvertMetaCategory;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
class AdvertsController extends BaseController {
/**
* Show the form for creating a new resource.
* GET /adverts/create
*
*/
public function create()
{
// New user instance
// dd(class_exists('\App\Models\User')); // Outputs FALSE
$userx = new User; // Throws an Exception
return View::make('adverts.create');
}
}

I have managed to resolve this on my own. It turns out you have to tell Laravel what class and table will be used for authentication (a.k.a your 'User' class). I didn't know this (plus this is an inherited project). Apparently the User class was defined in the root namespace (i.e. \User) and Laravel was configured to look for \User. But sometimes I see \App\Models\User in the code and it gave me the impression that User was under the same namespace as the other models since they were ALL in the app/models/ folder. I have corrected this problem in config/auth.php by changing:
'model' => 'User'
to:
'model' => App\Models\User::class
And adding namespace at the top of app/models/user.php:
namespace App\Models;
Finally I set an alias in config/app.php like this:
'User' => 'App\Models\User'
So that where ever I've been using User::blah will not break (forcing me to add use App\Models\User; everywhere!)

Related

Laravel ReflectionException : Class CustomerAccountController does not exist

I have this project on Laravel 5.7 and Voyager on WAMP with 2 issues, the first one is when I run php artisan route:list and the result is:
ReflectionException : Class CustomerAccountController does not exist
at C:\wamp64\www\cell_marketplace\vendor\laravel\framework\src\Illuminate\Container\Container.php:779
And actually the class exists and I'm using its functions on another processes and it's working, I've checked namespace, ran composer dump-autoload with no results.
The second one, I've created a BREAD on Voyager, and I got the model class and controller class, but when I go to the index of that resource again got this:
ReflectionException: Class DropOffController does not exist in \vendor\laravel\framework\src\Illuminate\Container\Container.php:779
And the controller exists and has a function that it's actually working, so I think that's related with the first one but if anybody can help I'd really appreciate it
The CustomerAccountController class:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Voyager\VoyagerBaseController;
use App\Models\CustomerAccount;
use App\Models\CustomerAccountTransaction;
use App\Models\Provider;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Stripe\Charge;
use Stripe\Stripe;
use TCG\Voyager\Facades\Voyager;
class CustomerAccountController extends VoyagerBaseController
{
[...]
I found the issue, it needed the absolute path to the controller, not the relative when configuring the BREAD for that resource, the project is in Laravel Voyager.

Laravel Lumen in Docker ClassLoader Exception

I installed a fresh Lumen in my docker container. Next I wanted to use eloquent so I activated it and it worked with some tests.
My problem now is if I want to get some Information about the model I've created, most of the times I get an error from the ClassLoader:
"include(/var/www/html/app/Models/Group.php): Failed to open stream: No such file or directory"
The file is obviously there, if I start the request again 1-2 / 10 times it worked. Finds the class and loads all things.
The Model itself has no special stuff in it, just an empty class.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Group extends Model
{
}
In my controller I'm using the model like this:
<?php
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\Group;
class TestController extends Controller
{
public function getGroups(Request $request): JsonResponse
{
return response()->json(['groups' => Group::all()]);
}
}
It looks like it has something to do with docker?
Hope someone can help.
Thanks in advance!

Problem with packages after Laravel namespace change to App\Models

So I used the namespace models shift to make the change from App to App\Models.
One of the packages in use (Ticketit) is calls App\User in it and I need a way to override this.
The offending file is:
\vendor\kordy\ticketit\src\Models\Agent.php
How can I override the "use App\user;" line there?
From what I am looking at in the source code, that is hardcoded, so you cannot do anything then... I am not sure if you could "fake" the App\User to point to App\Models\User using composer but that would be nasty, same if you created a class on inside app folder with name User and it only extends the model like this:
namespace App;
use App\Models\User as UserModel;
class User extends UserModel { }
That is nasty but maybe is a solution for you...
Edit: If you read carefully the documentation (please do so next time you use a package), it already says to make sure that App\User exists...
It tells you to do:
namespace App;
class User extends Models\User {
//leave this empty
}
For more info, see this github issue or create yourself a new one.

Laravel Model not being found

Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place

Class not found, Laravel Observer

I'm in the process of switching from Laravel 4.2 to Laravel 5, not sure if that is relevant, but I am getting an error:
"Class 'library\observers\UserObserver' not found"
and I have no Idea what the problem is, as far as I can see ( through my frustration mind you ) is that everything is in its right place, name spaces, folders, class names etc.. and I've ran the artisan dump autoload command twice now. the class is an observer which modifies user input on save. here is my code:
UserObserverServiceProvider.php:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use library\observers\UserObserver;
use App\Models\User;
class UserObserverServiceProvider extends ServiceProvider
{
public function boot()
{
User::observe( new UserObserver );
}
public function register(){}
}
UserObserver.php:
<?php namespace library\observers;
use library\Facades\Geo;
use Geocode;
use State;
use City;
class UserObserver{ code for user observer }
app.php configuration for service provider:
'App\providers\UserObserverServiceProvider',
All of these things were working together before the switch, what am I missing?
I left the App out of the namespace and path for use, works now, thanks!

Categories