LARAVEL 9 Target class does not exist - php

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.

Related

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;

Symfony\Component\Debug\Exception\FatalThrowableError Class '\App\Admin' not found

I am using laravel-5.7. i am making multi auth system I'm getting the following error
Symfony\Component\Debug\Exception\FatalThrowableError Class '\App\Admin' not found
Suppose you have Model class called Admin in the App folder, then you are calling it somewhere in the controller.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
//
}
Your controller will be
<?php
namespace App\Http\Controllers;
use App\Admin;
use Illuminate\Http\Request;
class YourController extends Controller
{
//Your code goes here
}
The answer is in the error that you get. You either don't have a class in your app directory named Admin or you don't have the namespace on top of your class.
<?php namespace App;
class Admin
{
}
or if your error is in a different class then you need to import it on top.
...
use PATH_TO_THE_ADMIN;
class YourClass {}
Most of the times when your class is not found, is basically as a result of not importing your namespace or typo error. so checke the two before anything else

Laravel can't find class in namespace

I using Laravel, I have a Model class under App/Models
<?php
namespace App\Models;
class TodoList extends \Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
In my Controller I have included the namespace as follows:
namespace App\Http\Controllers;
...
use App\Models\TodoItem;
use App\Models\TodoList;
class TodoListController extends Controller
My method looks like this:
public function show($id)
{
$list=TodoList::findOrFail($id);
return \View::make('todos.show')->with('list', $todo_list);
}
but when I call to a request i get the error:
FatalErrorException in TodoListController.php line 75: Class
'App\Models\TodoList' not found
Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.
I'm not sure, just try this :
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class TodoList extends Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
Here is the code from the docs, but for your example. Note the use Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TodoList extends Model {
//insert public function listItems() here
}
Hope this is helpful!
Make sure the file is in the correct folder and has the same name caption as the Class you want to import.
If the caption of the file "TodoList" is not TodoList.php but Todolist.php for example, Laravel wont find it.
Depending on your setup running "composer dump" might help to refresh autoload files.

Using Laravel 5 Model

I have created a model using
php artisan make:model Sessions
which creates a model named Sessions in the App\ folder. My Model (Sessions) and the controller(School) calling the model are below
Model (Sessions):
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model {
//
protected $table = "sch_sessions";
protected $primaryKey = "session_id";
}
Controller (School)
<?php
namespace App\Http\Controllers;
use App;
class SchoolController extends Controller {
public function sessionManagement(){
$Sessions = Sessions::all();
}
}
I get the following error
FatalErrorException in SchoolController.php line 7: Class
'App\Http\Controllers\Session' not found
I've tried putting the model in a folder and "use"ing it but nada. I'm stumped as to why the error is referring to a controller in the first place. Can anyone see something I don't (can't) please?
add on the top
use App\Sessions;
or use it with prefix
\App\Sessions::all();
If user Ali Mohammed's answer doesn't clear it up for you, it looks to me like your error is happening elsewhere. App\Http\Controllers\Session note Session is not plural.
Are you attempting to use the Session class elsewhere in your controller? If so, make sure to prefix it so it's using the Session declared in the global namespace. You can do this with \Session.

How do I call a model in Laravel 5?

I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
I have a controller named TestController and it resides in \app\Http\Controllers
This is the controller:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
FatalErrorException in TestController.php line 10: Class
'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
Thanks in advance...
You have to import your model in the Controller by using namespaces.
E.g.
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1); or import it first with use App\diamonds; and use it like you already did.
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd() (dump and die) instead of var_dump to see a nicely formatted dump of your variable.
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Try adding the following lines above your class decleration in your controller file...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php)
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
use Illuminate\Database\Eloquent\Model;

Categories