I'm trying to use Laravel packages. I created MyVendor/MyPackage
Routes, controllers, filters are already working. This is the classmap of my package:
"classmap": [
"src/migrations",
"src/controllers",
"src/seeds",
"src/models"
],
This is how looks my model:
namespace MyVendor\MyPackage\Models;
class MyModel extends \Illuminate\Database\Eloquent\Model {
}
And this is the code inside my controller which is in namespace MyVendor\MyPackage.
$test = new models\MyModel;
I'm getting this error:
Class 'MyVendor\MyPackage\models\MyModel' not found
I can't figure out why. I'm new with namespaces so maybe it is something related to this.
I tried with composer update, composer dump-autoload (inside my package) and still can't find my models.
If I get the declared classes with get_declared_classes() I can't see my model there.
The problem is that my model classes are not autoloading.
Try this:
Create models directory inside your package and add it to the package's classmap
Add a model YourModel.php with the following:
<?php
// Note no namespace
use \Illuminate\Database\Eloquent\Model as Eloquent;
class YourModel extends Eloquent {
//
}
Run composer dump-autoload from your package directory first and then root directory
Test your model by putting this at the top of your routes.php file:
<?php
$testModel = YourModel::get();
die(var_dump($testModel));
?>
These works for me on Laravel 4.2
<?php namespace Vendor\Package;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Product extends Eloquent {
...
}
Related
Good day, i'm trying to get the result from my model that called with Mainmodel through my controller, my controller is MainController.
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Mainmodel;
class MainController extends Controller
{
function index(){
echo "Kok, direct akses sih?";
}
function get_menu(){
$menu = app\Mainmodel::request_menu();
dd($menu);
}
}
Here is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Mainmodel extends Model
{
function request_menu(){
$menu = DB::table('menu')
->orderBy('[order]', 'desc')
->get();
return $menu;
}
}
my routes
Route::get('menu','MainController#get_menu');
with my script above i get this
FatalErrorException in MainController.php line 17: Class
'App\Http\Controllers\app\Mainmodel' not found
how can i fix this ? thanks in advance.
Note: I'm bit confuse with laravel. I'm using codeigniter before. And i have a simple question. In laravel for request to database should i use model ? or can i just use my controller for my request to database.
sorry for my bad english.
I would imagine it's because your using app rather than App for the namespace.
Try changing:
app\Mainmodel
To:
App\Mainmodel
Alternatively, you can add a use statement to the top of the class and then just reference the class i.e.:
use App\Mainmodel;
Then you can just do something like:
Mainmodel::request_menu();
The way you're currently using you models is not the way Eloquent should be used. As I mentioned in my comment you should create a model for each table in your database (or at least for the majority of use cases).
To do this run:
php artisan make:model Menu
Then in the newly created Menu model add:
protected $table = 'menu';
This is because Laravel's default naming convention is singular for the class name and plural for the table name. Since your table name is menu and not menus you just need to tell Laravel to use a different table name.
Then your controller would look something like:
<?php
namespace App\Http\Controllers;
use App\Menu;
class MainController extends Controller
{
public function index()
{
echo "Kok, direct akses sih?";
}
public function get_menu()
{
$menu = Menu::orderBy('order', 'desc')->get();
dd($menu);
}
}
Hope this helps!
You can solve it by different solution. The solution is you don't have to call request_menu(); you can get it in your controller.
MainController
use use Illuminate\Support\Facades\DB;
public function get_menu(){
$menu = DB::table('menu')
->orderBy('Your_Field_Name', 'DESC')
->get();
dd($menu);
}
I am beginner in laravel. And I am using Laravel 5.0.16 in my wamp server. I have been learning laravel by free video tutorial available in laracasts.com. I have been trying to fetch data from database. I have checked that my app is already connected to database.
I do have below structure in app folder:
-app
-Http(folder)
-Other folders (folder)
-Article.php (file)
-User.php (file)
In side Http folder:
-Controllers (folder)
-Middleware (folder)
-Requests (folder)
-Kernel.php (file)
-routes.php (file)
In Controllers folder:
-ArticleController.php (file)
Below is code in side routes, controllers and model file:
/*routes*/
Route::get('articles','ArticleController#index');
/*Controller file*/
use App\models\Article;
namespace App\Http\Controllers;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Where users is DB table with fields.
I am getting below arror:
FatalErrorException in ArticleController.php line:
Class 'App\Http\Controllers\Article' not found
I have check other SO forums but they didn't help me, can anyone suggest me what am I missing?
There are two issues here. One the namespace declaration should happen before any use statements.
Second your models uses the model namespace but your models aren't in a model directory. The namespace should match the directory structure. So you either need to change the namespace to use App\Article (also change the namespace in the model file) or move the model files into a models directory.
So to fix this without moving files update the code to look like this
namespace App\Http\Controllers;
use App\Article;
use App\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller {
public function index()
{
$users = Article::all();
return $users;
}
}
/*Model file - Article.php*/
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = 'users';
protected $fillable = ['id','firstname', 'lastname', 'email','reg_date'];
}
Add this line to the top of a controller:
use App\models\Article;
Or use the full namespace when working with this model:
\App\models\Article::all();
I'm quite new to Laravel.
I have no DB connection but I'm trying to separate the logic in the Controllers placing it in the Model in order to create fat models and slim controllers.
When doing it I realized I have to make use of common functions in different models. I've seen they usually place those classes in a app\lib\, but I guess that's just for controllers to access them? I can not seem to access them from m model:
<?php
//in app/lib/MyLog.php
class MyLog{
//whatever
}
Then in m model:
//in a model
MyLog::setLogApi($url);
The error I'm getting:
PHP Fatal error: Class 'MyLog' not found in C:\inetpub\wwwroot\laravel\app\models\Overview.php on line 80
If you include your Model like this in your Controller use App\MyLog;
Then You should have the MyLog.php file inside app\MyLog.php
Update : As the OP wants to access some common functions from any Model
Then Mutators should help you do that
Here is the similar example given over there
public function convertToLower($value)
{
$this->attributes['yourLowerString'] = strtolower($value);
}
Ensure that your model has a namespacing. If your MyLog class has a namespace e.g.:
<?php namespace App\Logging;
class MyLog {
}
Then you can call that in your controller as follows:
<?php namespace App\Controllers;
use App\Logging\MyLog as MyLog;
class MyController {
protected $logger;
public function __construct() {
$this->logger = new MyLog;
}
}
It could be possible that you have to do a composer dump-autoload. This maps namespaces and classes to the right files.
You should use namespaces, it's good practice for modern PHP.
File app/lib/MyLog.php
namespace App\Lib;
class MyLog {
// class functions
}
File /app/models/Overview.php
namespace App\Models;
use App\Lib\MyLog;
class Overview {
// class functions
}
You can use short aliases for fully namespaced classes. Aliases are stored in /app/config/app.php, find part
"aliases" => array(
'App' => 'Illuminate\Support\Facades\App',
.
.
. );
At the end of array add your new alias for MyLog class:
'MyLog' => 'App\Lib\MyLog'
And now in your /app/models/Overview.php you can use shorter alias:
namespace App\Models;
use MyLog;
class Overview {
// class functions
}
Routing in Laravel5 seems to be a major problem for me.
I was hoping to follow this example using the composer mapping
https://mattstauffer.co/blog/upgrading-from-laravel-4-to-laravel-5#namespacing-controllers
To avoid any issues with models or facades.
But when I route to this:
Route::get('school/test', 'school\SchoolController#index');
Error
ReflectionException in Container.php line 776: Class school\SchoolController does not exist
The SchoolController is in the HTTP/controllers/school folder:
namespace School
class SchoolController extends Controller{
public function index() {
return "hello";
}
}
RouteServiceProvider:
protected $namespace=NULL
composer is set for the HTTP/controllers
"classmap": [
"database",
"app/Models",
"app/HTTP/Controllers"
]
and works with routes such as this:
Route::resource('courses', 'CourseController');
So the router is just not finding files in a subfolder. I wonder what the problem is?
It seems the only option is
RouteServiceProvider
protected $namespace = 'App\Http\Controllers';
Composer.json
`"classmap": [
"database",
"app/Models"
],
HomeController in the App\Http\Controllers;
namespace App\Http\Controllers;
use App\Models\Course;
class HomeController extends Controller {
public function index()
{
$courses =Course::orderBy('created_at','DESC')->with('school')->paginate(12);
}
But this means I need to add 'use App/...' for over 100 controller files, with varying models!
I appreciate help so far but I'm really looking for method one if possible, as two will involve placing all the model maps in each controller (lots of code). Unless there is a global way to map all the models in one file?
Someone suggested Alias but this doesn't work.
To re-iterate the issue. Routing fails for Controllers subfolders using composer for mapping
Laravel will by default search for controllers in App\Http\Controllers. You can change that namespace by editing App\Providers\RouteServiceProvider:
protected $namespace = 'App\Http\Controllers';
In your case, since you want no "base namespace" at all, set it to null:
protected $namespace = null;
Created directory structure: app/Controllers/Folder (the names don't really matter as long as they match with the rest)
Created controller in Folder: TestController.php (namespace Folder;)
Edited autoload > classmap in composer.json and added "app/Controllers"
Run composer dump-autoload
I have decided to use subdirectories for my controllers so that I can manage the application better, but unfortunately I am getting an error,
App
controllers
bdm
LeadController.php
models
LeadsModel.php
this is my LeadController.php code
<?php
namespace bdm;
use Database\Eloquent\Model; //still nothing
class LeadController extends \BaseController
{
}
This is my model code
<?php
class LeadsModel extends Eloquent
{
//code here
}
This is my Route code
Route::group(
array
(
'prefix' => 'bdm'
),
function()
{
Route::get('lead/index','bdm\LeadController#index');
Route::post('lead/get_random_lead','bdm\LeadController#getRandomLead');
}
);
The errors I am getting is:
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'bdm\\LeadsModel' not found","file":"C:\\xampp\\htdocs\\holbornasset\\crm\\app\\controllers\\bdm\\LeadController.php","line":55}}
Your LeadsModel is under global namespace. Your LeadController is currently under bdm namespace.
You have two option to call LeadsModel from LeadController
Add namespace bdm to your LeadsModel
Use backslash \ to access LeadsModel
\LeadsModel::find(...);