Use a model instance in different controller - Laravel 5 - php

There is a HomeController in app/Http/Controllers and a Player model in app.
HomeController:
namespace App\Http\Controllers;
Player:
namespace App;
use Illuminate\Database\Eloquent\Model;
Double-checked, the Player model gets PSR-4 autoloaded. I've tried to use various versions in HomeController, but it keeps trying to look for Eloquent class (which Player extends) in wrong paths. Also, in the tested HomeController method, instantiating the model is foggy, do I need to prefix the namespace now? because with or without use command it does not work.

Realized that I had to alias this path to Eloquent to make it work:
use Illuminate\Database\Eloquent\Model as Eloquent;

Related

LARAVEL 9 Target class does not exist

After creating a Model and a Controller using artisan and defining the action in the controller.
I created a route Route::get('/showStudents', 'StudentsController#index');
But the problem is the controller is not defined by laravel .
I checked the possibility of typos so many times , namespaces ,controller name ,model name
that path was so usefull , I asked a friend who said that channging the controller name after creating it using artisan makes that error , but thing is i recreated the controller name another time and i left the original name . But unfortunately that didn't help
thank in advance
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentsController extends Controller
{
//
}
models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Students extends Model
{
use HasFactory;
public funtion index(){
echo 'hhh';
}
route
Route::get('/showStudents', 'StudentsController#index');
I tried this syntax in
[StudentsController::class, 'index']
instead of
'StudentsControlle#index '
and it worked.

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 'app\login' not found in laravel 5.8

I am using laravel 5.8 and have an issue with model class. Since model is placed directly in app folder.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Login extends Model
{
//
}
In Controller, I am using
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Login;
use DB;
I initialized model class object in MyController.php as
$table_ob=new Login;
but facing this issue "Class 'app\Login' not found" when i submit form to controller.
It's minor spell mistake
use app\Login;
to
use App\Login;

Lumen - Namespace issue

I am in trouble with namespaces in lumen/laravel. My project structure is following:
App/Http/Controllers
ExampleController
If I would call my Controller by a route:
Route::get('/', 'ExampleController#test');
I've got the exception, that this class App\Http\Controllers\ExampleController is not found! In my ExampleController I've signed it with namespace App\Http\Controllers;
Is there any explaination, because I dont changed the default namespace and I need to group it with namespace => App\Http\Controllers. If I use that namespace grouping, it would work?!

Laravel - Class/Model not found

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

Categories