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
Related
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.
I'm actually making a laravel CRUD app.
So there is this model called User which has been created, but when I try to use it in a controller (In this case, HomeController.php), it says:
Here is line 28 from the controller:
I'm sorry if this question already exists but I've searched everywhere for a solution but could not find it.
Thank you.
Ideally, you should post the line you are importing the model, and the first few lines of your model.
But I will try to explore the possibilities of error.
Is your model inside a specific folder (just like app/MyModels/User.php) or just inside the default place when you use php artisan make:model? Remember to put the exact path in namespace line inside of model.
In the first line of your model you should have the path of your model with namespace. If the model is in its default directory, you should have the line below, but if it is inside of a folder, you have to put the folder path after de App:
namespace App;
Verify if you model is extends of Model:
class User extends Model {
In your controller the use line have the same path of namespace line in model, if you model is in default path, should be like this:
use App\User;
Remember that: the App is the namespace of your project, if you used the php artisan app:name command your namespace is no longer an App, and you should change it if you have the old name somewhere. If you using linux, remeber of the case sensitive.
If it does not work, put into the post the codes of the files I've listed so we can help.
in the begining
<?php
use App\User;
Initially, this code worked, but it doesn't work anymore. I don't know what is causing the issue.
The error is :
FatalErrorException in AdminController.php line 64:
Class 'App\Category' not found
AdminController code is:
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Category;
use View;
class AdminController extends Controller
{
public function article_get()
{
$categories = Category::latest()->get();
return View::make('create.article')->with('categories',$categories);
}
}
My model Category is located at App/Models/Category.php.
What I've tried:
Change from use App\Category; to use Category, to use \Category, to use App\Models\Category.
composer dump-autoload.
A few hours ago I had a working project, but now my models are not found.
Because Laravel uses PSR-4 autoloading, you need to make sure your namespaces match the real directory structure.
You say that your Category model is located at App/Models/Category.php so its namespace should be App\Models. Then, in your controllers you would use App\Models\Category.
I had also faced the same error when Models are called inside Controllers or Seeders.
The best solution is to import your Model inside the Controllers.
Add the below line at the top of your AdminController.
use App\Models\Category
This is applicable for all Classes where you try to call your models.
Do it like this and it must work:
use App\Models\Category
and make sure that the model Category is inside a folder named Models.
Your code seems fine.
May be it has an issue with namespace in Category model.
if you use this code for Category Model in your AdminController controller:
use App\Models\Category;
then your Category model itself has this namespace
namespace App\Models;
Check if your model namespace is still there or not.
Thanks
App\Models\Category
PLease Check This twoThings
1.Importing namespace App\Http\Controllers;
2,Check Controller Letter is in Upper Or LowerCase
like this
use App\models\Category----->❌❌❌
use App\Models\Category----->✅✅✅
use App\Models\Category
//check your models is capital or small letter sometimes issue like that is a big problem
The Laravel documentation clearly describes how to change your routes if you nest your controllers in folders. It seems REALLY simple, and yet I'm still getting an error. Here's the error:
"Class App\Http\Controllers\Input\InputController does not exist"
^That path looks 100% correct to me. What gives?
File Structure:
-Controllers
--Auth
--Input
---InputController.php
Routes:
Route::get('input', 'Input\InputController#getInput');
InputController:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
Thanks for any help!
Change Controller namespace from
namespace App\Http\Controllers
to
namespace App\Http\Controllers\Input
namespace needs to be changed to the directory your controller is in 'App\Http\Input'
You need to pull in Controller with use App\Http\Controllers\Contoller so that you can extend it.
<?php
namespace App\Http\Controllers\Input;
use App\Http\Controllers\Controller; // need Controller to extend
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
you should try running a couple commands in your base dir from your terminal (shell/prompt):
composer dump-autoload
or if you don't have composer set as executable:
php composer dump-autoload
and then:
php artisan clear-compiled
This way your laravel would prepare everything again "from scratch" and should be able to find the missing controller class.
Basically laravel generates some additional files to boot up faster. If you define a new class it doesn't get included into that "compiled" file. This way your class should be "introduced" to the framework.
I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.