Same name for Laravel controller and model? - laravel

I had a model Entry and had no problem with it.
After I created a new Controller with same name, the model stopped working.
When I execute Enrty::where(... a Fatal error occurs:
Call to undefined method entry::where()

Define Controller
<?php namespace Controllers;
class Entry extends \BaseController {}
Define Model
<?php namespace Models;
class Entry extends \Eloquent {}
Now, you can use these classes if you import/alias them
use Controllers\Entry as EntryController;
use Models\Entry as EntryModel;
EntryController::method();
EntryModel::anotherMethod();
Note: this code serves to show the principle.

without changing/renaming namespaces
simply use as keyword
use Models\Car as CarModel;

Related

laravel 8 Target class [ColorController] does not exist

I created ColorController Class using
php artisan make:controller ColorController
The class has been created successfully and exists in the App/HTTP/Controller folder.
I defined route like this
Route::get('color/text', [ColorController::class, 'text']);
And a Method in the Contoller Class like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ColorController extends Controller
{
public function text()
{
return 'Color Controller';
}
}
I visited the like this
http://localhost:8000/color/text
It shows the error message as below
Illuminate\Contracts\Container\BindingResolutionException
Target class [ColorController] does not exist.
http://localhost:8000/color/text
Where did I make the fault? where to correct the process?
I have found the Solution like this...
I had to add this line above in the web.php file
use App\HTTP\Controllers\ColorController;
Now all works fine.

Class App\Http\Controllers\CategoriesController does not exist

hi m trying to get view of page but it says: Class App\Http\Controllers\CategoriesController does not exist
pic of folder structure https://ibb.co/gMBvwDJ
Route:
Route::match(['get','post'],'/admin/categories/index','CategoriesController#Category');
controller:
public function index()
{
return view('admin.categories.index');
}
First of all you use a wrong definition of the route match, so try this:
Route::match(['get','post'], 'CategoriesController#index');
and second, make sure that in your CategoriesController you use the right namespace, which should be:
namespace App\Http\Controllers;
at the very top of the class.
It gives you that error because the controller couldn't find the class you are calling so in order to fix the problem, in the top of your controller add
use App{ModalName};
for example if your model for this is called categories,
use App\categories;
as well as add
namespace App\Http\Controllers;
Please make sure your controller is placed in below directory structure or else you have to fix the namespace problem

How import custom class in controller Laravel?

I have created a new directory Library in root of Laravel.
Inside I put the file with class:
class My {
//
}
So, in controller Laravel I try to get access to this class:
App\Library\My
But Laravel does not determine this path.
This is my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use App\Library\My;
class HomeController extends Controller
{
//
}
A complete and functional example based on the posts here:
1 - Folder and file - Create a folder under app/, in this example we will create a folder called Library.
We will also inside the folder create a file with the name of your class, here we will create a class called My.
So we will have app/Library/My.php
2 - Class and method - Now, for testing, inside the class create a static method called myMethod
<?php
namespace App\Library;
class My
{
public static function myMethod()
{
return 'it\'s work!';
}
}
3 - Controller - Now at the beginning of the Controller, we will declare the namespace of your Class with use:
<?php
namespace App\Http\Controllers;
use App\Library\My;
//rest of the controller code
Finally, to create an instance of the My class, in Controller, a new statement must be used:
//rest of the controller code
public function index()
{
$whatever = new My;
return $whatever::myMethod();
}
As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.
<?php
$fOne = new \App\library\functions;
$isOk = ($fOne->isOk());
?>
You should create Library folder inside app folder
namespace App\Library\My
app folder is alrdy used psr-4
In your controller
use App\Library\My as My
It's work for me. Hope this answer is helpful
You have to properly namespace your every class.
So you can import your class with use keyword, like so
use App\Library\My;
....
$my = new My();
Or if you've conflicting class name then you can use as keyword to alias the classname while importing
use App\Library\My as MySecond;
....
$my = new MySecond();
And if you want to directly access your class within the method then you can access it like so.
$my = new \App\Library\My();
Note: The leading \ means App was declared in the global scope.

Laravel 5 and models

I have a new installation of Laravel 5. The problem is that it's not recognizing my model classes. I will keep it very simple for solution purposes.
Route::get('test', function() {
$test = boxstyle::all();
....
}
My model is in the app directory
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Boxstyle extends Model {
protected $table = 'boxstyle';
protected $primaryKey = 'key1';
}
I am getting:
class boxstyle not found error
I've been searching all over the internet and can't find a solution. This installation is fresh. This isn't magic and I suspect a configuration issue but I can't find a solution. This works fine in Laravel 4.2 so I know it should work but not working in L5.
Your model is defined inside the App namespace. If the code accessing the model is not in the same namespace as the model, you need to qualify it.
Route::get('test', function() {
$test = \App\Boxstyle::all();
}
Laravel 4 did not define the models and controllers inside namespaces. Your models would have been defined inside the global namespace, so any code also in the global namespace (like your controller) would not need to qualify the model. However, Laravel 5 has made the push to put most everything inside namespaces.
To create a model in Laravel 5 try this,create a folder name "Models" folder under your App folder, now for instance you want to create a Model class for Boxstyle ...under your Models folder create a file name
"BoxstyleModel.php" inside your BoxstyleModel.php should look like this and make sure your inside the folder to namespace your model under the App\Models
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BoxstyleModel extends Model {
public static function say_hello(){
return "Hello";
}
}
in your Boxstyle Controller to be able to use your BoxstyleModel added this code at the top of your BoxstyleController use App\Models\BoxstyleModel;
now everything should be the same like Laravel 4.X.

Extend CodeIgniter Base Model causes Fatal error: Class 'MY_Model' not found

I want to extend CI_Model:
<?php
class ModelBasic extends CI_Model
{
}
?>
It won't be autoloaded unless under such a path:
./application/core/MY_Model.php
It seems name of MY_Model.php is mandatory. Now my problem is that when I create MY_Model.php CodeIgniter Expects me to have a model under name of MY_Model which I want to avoid. Can I have my own custom model name without making a fake class to suppress this error?
Fatal error: Class 'MY_Model' not found in /var/www/CodeIgniter/system/core/Common.php on line 174
The quickest way to achieve naming your own custom base model(s) without any additional modification is to create the file application/core/MY_Model.php, define an empty MY_Model class, and then create your own custom class(es):
class MY_Model extends CI_Model {}
class ModelBasic extends MY_Model {
// Your code here
}
// Define more than one if you want.
class ModelComplicated extends MY_Model {
// Your code here
}
Other options include:
Extend the Loader class, and replace the model() method with your own modified version. This could be complicated (I haven't explored it fully), since the actual error is occurring in system/core/Common.php :: load_class(), which, in the model's example, is instantiating the base CI_Model class, as well as MY_Model if it finds one (which it automatically looks for by default, like other base classes).
Create / Add an autoloader that follows your own rules for loading core classes (may be complicated if you try to autoload more than just models -- really depends on how you want to set your app up).
Extend the Loader class and re-write that part of the code.

Resources