How to fix Class 'app\hotelroom' not found error in laravel? - php

Im new to laravel , i have created migration and the table (hotelroom) earlier but forgot to create the model for the same ,bu now after creating the model and trying to retrieve the data , im getting above error
This is where it highlights the error
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\hotelroom;
class manage_roomscontroller extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$arr['hotelrooms']=hotelroom::all();
return view('admin.rooms.index')->with($arr);
}
}
Arguments
"Class 'app\hotelroom' not found"
``````````````````````````````````
model ( located under app directory)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class hotelroom extends Model
{
}
can someone tell me where the issue is? is it because i have created the model later ?( not at the same time while I created the migration). or its some other issue?

This error is because of app which will not work, you need to put proper namespace name as like below
use App\hotelroom;
The app directory is namespaced under App by default. You can open composer.json file and see that the app directory is set up for psr-4 autoloading:
"psr-4": {
"App\\": "app/"
},

Related

Laravel Lumen in Docker ClassLoader Exception

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!

FatalThrowableError Class 'App\Models\Patient' not found in laravel 5.4

I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.
In Model Patient:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
//
public function getPurchaseOrder(){
return "Hello World";
}
}
In Controller PatientController:
namespace App\Http\Controllers;
class PatientController extends Controller
{
protected $patientModel;
public function __construct(Request $request, PatientInterface $patient)
{
parent::__construct($request);
$this->patientModel = new \App\Patient();
}
public function test(){
echo $this->patientModel->getPurchaseOrder();
}
}
It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:
FatalThrowableError Class 'App\Models\Patient' not found
Any help will be appreciated.
When you move a class to a different directory, for it to be loaded by composer with the PSR-4 standard, you must also update the class' namespace to match.
namespace App\Models;
In addition, when you run the make command, you can include a namespace in that to automatically put it in the directory with the correct namespace:
php artisan make:model Models\\Patient

Reflection Exception : Class ClassName doesn't exist

I am using Laravel 5.5. I have added a custom directory inside App folder in my workspace. So, the folder structure is:
Inside App\Bishwa\Transformers there are two PHP files:
Transformer.php
LessonTransformer.php
Those files look like follows:
Transformer.php
<?php
namespace Bishwa;
abstract class Transformer {
public function transformCollection(array $items){
return array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
LessonTransformer.php
<?php
namespace Bishwa;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}
Then Inside LessonsController.php I have the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Lesson;
use Bishwa\LessonTransformer;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
dd('ok');
}
While running action of the controller, It gave me an error message saying:
Reflection Exception: Class Bishwa\LessonTransformer does not exist
I have tried composer dump-autoload, restarting the server again but none of them helped. Am I doing wrong while Namespacing or What?
Change your namespace of the files in your custom directory to App\Bishwa.
Well thanks to Jerodev and Jack. Since, both of them were write I decided to write myself a combined solution to this problem.
1st Solution:
In case of custom namespaces and custom classes I have to include the path to classname in Composer.json file in the following portion:
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Bishwa/Transformers"
],
"psr-4": {
"App\\": "app/"
}
2nd Solution:
Changing NameSpace of my files to custom directory App\Bishwa .
Namespace of transformer.php and LessonTransformer.php now becomes:
namespace App\Bishwa\Transformers;
While using in LessonsController:
use App\Bishwa\Transformers\LessonTransformer;
Once again, big thanks to Jerodev and Jack. Its my silly mistake, that I couldn't figure that out.

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.

Laravel 4 - Repository Interface not found

Just started working through laracasts and trying to move on from direct eloquent use in the controllers.
I have implemented everything that I need to but hitting this error:
Class tva\Repositories\VehicleRepositoryInterface does not exist
My folder structure is:
app/
tva/
repositories/
VehiclesController:
use tva\Repositories\VehicleRepositoryInterface;
class VehiclesController extends \BaseController {
protected $vehicle;
public function __construct(VehicleRepositoryInterface $vehicle)
{
$this->vehicle = $vehicle;
}
}
In the repositories folder:
VehicleRepository:
namespace tva\Repositories;
class VehicleRepository implements VehicleRepositoryInterface {
}
VehicleRepositoryInterface:
namespace tva\Repositories;
interface VehicleRepositoryInterface {
}
And also updated my composer.json:
"psr-0": {
"tva": "app/"
},
To me, this should work?
Issue solved, instead of using psr-0 I added the directory to the classmap and all issues solved.

Categories