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
Related
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;
Where to save new model file created in laravel 5.3 in /app/http folder or in /app folder
And how I will call the method in it from controller ?
How to include the model in controller?
Laravel 5.3
Save them in app/models folder and you can access it by constucting it,Make sure you have ran composer dump-autolad
public function __construct(YourModel $model) {
parent::__construct();
$this->model = $model;
}
in your code simply model->test();it should work.
You create the folder Modelswherever you want and add your files as you wish, just make sure to properly namespace it. As long as Composer can autoload the class.
You can manually create app/Models/User.php and just make sure to namespace it:
namespace App\Models;
class User {
// your code
}
And to call the model method from a controller use the namespace of the model:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\YourModel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class YourModelController extends Controller
{
public function whatever(){
$yourmodel->method();
}
}
Why yii2 throw exception, when I try use console controller? Code:
<?php
namespace app\commands;
use yii\console\Controller;
class Hashtag extends Controller
{
public function actionIndex($search = 'test')
{
echo $search;
}
}
Controller located in: app\commands\HashtagController
When using terminal: php yii hashtag
Exception 'yii\base\UnknownClassException' with message 'Unable to find 'app\commands\HashtagController' in file: /var/www/html/yiitask/yii2/commands/HashtagController.php. Namespace missing?'
in /var/www/html/yiitask/yii2/vendor/yiisoft/yii2/BaseYii.php:291
Other controller in this folder that was created before, working well.
Your namespace is wrong set the namespace for console controller properly eg: (depend where you have you console controller directory)
namespace app\console\controllers;
then could be is missing controller
class HashtagController extends Controller
{
instead of
class Hashtag extends Controller
{
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.
I have create Helpers folder inside app, then I have created php file amchelpers.php ---> app/Helpers/amchelpers.php
amchelpers.php code:
<?php namespace App;
class AmcHelper {
static function displayString($string){
return $string;
}
}
then added these lines to composer.json
"files": [
"app/Helpers/amchelpers.php"
]
then run this command:
composer dump-autoload
then added 'Helper' => app_path() . '\Helpers\AmcHelper' to aliases array in config/app.php file.
in my controller I have below action (this action defined in route.php):
use Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}
when run the page http://localhost:8080/easy_marketing/public/displayMyString
I Got:
ErrorException in compiled.php line 6367: Class 'C:\wamp\www\easy_marketing\app\Helpers\AmcHelper' not found
you have written user Helper instead of use Helper
or
another way to achieve this is
Laravel 5 App directory is autoloaded by default with its folder, what you have to take care is add namespace followed by directory name,
so directory structure is App --> Helpers
so your name space must include App\Helpers
try following code
<?php namespace App\Helpers;
class AmcHelper {
static function displayString($string){
return $string;
}
}
and when you are using this class in another class write this after namespace declaration
use App\Helpers\AmcHelper as Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}