Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.
Related
I am trying to extend the package controller in my base laravel controller. Tried importing class using below code which shows error as class not found.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ProductController as ControllersProductController;
use App\Imports\ProductsImport;
use AvoRed\Framework\AvoRedProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Avored\Framework\Catalog\Controllers\ProductController;
class ProductControllers extends Controller
{
private $avored_product;
public function __construct(ProductController $p) {
$this->avored_product = $p;
}
public function index() {
echo $this->avored_product;
}
Tried multiple options by researching on it couldn't find the same. Request all to please guide me with same.
Could you share with us the exact error message? From your code snippet I can't see which class couldn't be found.
And which Avored package are you referring to? I guess avored-laravel-ecommerce?
If you want to extend the ProductController-from the package, you have properly extend from that controller.
<?php
namespace App\Http\Controllers\Admin;
use Avored\Framework\Catalog\Controllers\ProductController as AvoredProductController;
class ProductControllers extends AvoredProductController
{
public function index() {
// Do your thing in here
}
}
You can now override the controller methods to your liking.
I tried to route with using name .
I already tried to solve with using Composer update , Clear Cache , Generate App key.
But it's showing fatal error .
Error View Message :
(1/1) FatalErrorException
Class 'App\Http\Controllers\Controller' not found
in ClassesController.php line 13
My Route Code Below :
<?php
Route::prefix('Classes')->group(function(){
Route::get('/add','ClassesController#index')->name('AddNewClass');
});
My Controller Code Below :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ClassesController extends Controller{
public function index()
{
return "Method Access";
}
In this event, I would first ensure that the file containing the class Controller exists in the same directory with the same namespace as the ClassesController, as implied in your code.
If the Controller does not exist, you can either create the class or simply remove the extends Controller and the line use App\Http\Controllers\Controller; from your code.
its worked for me.
usercontroller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*/
/* http://localhost:8080/laravelapps/blog/public/staff */
public function showProfile()
{
return 'new';
}
}
web.php
Route::get('/staff', 'UserController#showProfile');
http://localhost:8080/laravelapps/blog/public/staff
Check whether Controller.php file exists or not in your Controllers folder if not exists download or put from laravel project.
After that run following command php artisan app:name newproject
Make sure in the above line i have updated text 'newproject' that should be first line of your Controller.php file. For example: namespace newproject\Http\Controllers;
I am getting the error below even though my Controller class is present in my Controllers directory;
Fatal error: Class 'App\Http\Controllers\Controller' not found
Below is the content of my UserController controller (in the same directory, Controllers):
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function SignIn(Request $request)
{
if (Auth::attempt(['usernameEmail'=>$request['usernameEmail'],'password'=>$request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
public function getDashBoard()
{
return view('dashboard');
}
}
What am I doing wrong and how can I resolve it?
You get the Fatal error: Class 'App\Http\Controllers\Controller' not found
error because you have no such a class in your project at the derived location.
Instead of
use App\Http\Controllers\Controller; // this is irrelevant and causing the error you get
you should have
use Illuminate\Http\Request; // this is necessary for your SingIn method
as in:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
// your methods here
}
because one of your SignIn method does make use of it.
It you ran composer dump-autoload and it didn't fixed your problem you placed your controller in wrong folder. It should be inside app\Http\Controllers dir.
If it won't fix your issue, look at storage\logs\laravel.log where you can find more informations about the problem, and where your problem occured. Maybe you are defining routes using wrong classname or something like that.
I'm learning laravel but it isn't working out that well....
I've set my route in routes.php:
Route::get('/','WelcomeController#index');
Then I obviously have made a controller called "WelcomeController" and it looks like this:
<?php
class WelcomeController extends BaseController
{
public function index()
{
return view ('index');
}
}
?>
And then I've made a view called index with just some html text.
But when I go to localhost/public I receive the error:
FatalErrorException in WelcomeController.php line 3:
Class 'BaseController' not found
And when I say:
class WelcomeController extends Illuminate\Routing\Controller
It does not work!
What am I doing wrong.
You should try
use Illuminate\Routing\Controller as BaseController;
at the top of your controller file. That acts as an import
Two suggestions:
Run php composer dump-autoload to make sure the class mappings is fresh.
Add use Controller; in your use block. The modify your controller to extend it. Example:
class WelcomeController extends Controller {...
Controller is an interface in Laravel 4.*
In Laravel 5 use instead:
use App\Http\Controllers\Controller; according to the documentation here: http://laravel.com/docs/5.0/controllers
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;