I’m assuming this may be some convention I’m not understanding.
I have a model called Ingredient in the main App folder.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ingredient extends Model
{
protected $table = "ingredients";
}
Then I have a controller trying to call that app
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Config;
use App\Ingredient;
class IngredientController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return response()
->json([
'status' => 'ok',
'ingredients' => Ingredient::all()
]);
}
}
Everything seems to be ok. I get a 500 server error though:
include(C:\\Dev\\ScratchApi\\vendor\\composer\/..\/..\/app\/Ingredients.php): failed to open stream: No such file or directory
The file name of the model is Ingredient.php
Do I need to rename my file to Ingredients.php to meet a naming convention? Or why is this trying to call a file name different from the class name I’m telling it to look for?
I think it's because of autoload cache. So, Run following command on your project root directory,
composer dumpautoload
Hope it works.
Add option "-o" when run dump autoload.
composer dump-autoload -o
Try to use this:
composer dump-autoload
Related
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!
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
I am working in laravel 5.5. I have created a controller folder with name Abc and inside it created a controller file with name abc.php, inside this file I have written code:
namespace App\Http\Controllers\Abc;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\DB;
class abcController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function NewPromotion()
{
return view('new-promotion');
}
}
And in Route folder under web.php file I am calling its view file, like:-
Route::get('/new-promotion','Abc\abcController#NewPromotion');
In spite of defining in namespace I am still getting error i.e.
"ReflectionException (-1)
Class App\Http\Controllers\Abc\abcController does not exist".
What could be the possible issue?
You should make sure your file is located in app/Http/Controllers/Abc directory and has name abcController.php (case sensitive).
Also by convention class names start with big letter, so you should rather name your controller AbcController and have this file with name AbcController.php (and also update your route file to use valid case of this controller name).
Good way to create controller is use cmd (you have to be in project directory)
php artisan make:controller "Abc\abcController"
I've been trying unsuccessfully to resolve an error in a laravel 5.2 app (carfreak).
FatalErrorException in PropertyController.php line 85:
Class 'App\Models\CarModel' not found
I have moved the default user model to a folder app/models and made the necessary changes so that it's all working fine.
Now I have a new controller CarController, and a new model, CarModel that are just not working. It seems to be such a simple problem with namespaces, but I am unable to resolve it.
is the model in the models folder? Yes. carfreak\app\Models\CarModel.php
is the controller namespace correct? Yes... namespace carfreak\Http\Controllers;
does the controller reference the model? Yes...use App\Models\CarModel;
is the model namespace correct? Yes... namespace carfreak\Models;
I am able to create different versions of the error by playing with the CarController but no permutation I can think of has worked.
EDIT: Controller and Model added...
EDIT: More details added:
The irony of this is that I can php artisan make:model sanityCheck and it will create a model in the \app root (i.e. not in app\models)... and that model can be called just fine. If I put my CarController back in the root, and change it's namespace appropriately, it still doesn't work. It's almost like I have some stupid spelling error in the class name or something, but I've copied and pasted class names into my filename, class declaration, "use" declarations, etc. and it. still. doesnt. work. Aargh!
//this is carModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CarModel extends Model
{
//
/**
* The attributes that are mass assignable.
* #var array
*/
protected $fillable = [
'colourOfCar',
];
}
//this is carController
<?php
namespace carfreak\Http\Controllers;
use Illuminate\Http\Request;
//use \carfreak\app\Models\CarModel;
use App\Models\CarModel;
class CarController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
// validate the data
$this->validate($request, array(
'CarColour' => 'required|max:50'
));
// store in the database
$newCar = new CarModel;
dd($request);
}
}
This looks wrong use \carfreak\app\Models\CarModel; should be use App\Models\CarModel in this is carController
Casing is important on linux. The namespace generally is used in a PSR Aloader to find the file. And Linux filesystem is case sensitive. So the CareModel.php file should be located in App/Models/CarModel.php
But I never used Laravel...
Well, here it is. I solved it by asking myself this question: If I'm having so much trouble namespacing, referencing and organising my models, then maybe I can get artisan to do it for me.
The post that got me thinking differently was Mansoor Akhtar's advice here: How do I instruct artisan to save model to specific directory?
Get artisan to make the model in the right place first time.
php artisan make:model Models/CarModel
In the Controller, reference the model correctly
use name-of-app\Models\CarModel;
There may or may not be cache flushing involved in my problem. I was eventially restarting my XAMPP after every change to ensure no caching was involved. I also tried
php artisan cache:clear
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.