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(...);
Related
I'm using Lumen 8.3 ,wanted to use factory() function in my tests, it gives me
Undefined Function ,there is nothing useful in the Docs of Lumen
Am i missing something here?
class ProductTest extends TestCase
{
public function test_if_can_send_products_list(){
$products = factory('App/Products',5)->make();
$this->json('post','/payouts',$products)
->seeJson([
'created' => true,
]);
}
}
->
Error: Call to undefined function factory()
It's better to use direct class like that:
$products = factory(Products::class, 5)->create();
don't forget to add Products model usage (namespace).
Edit
You should create Factory:
<?php
namespace Database\Factories;
use App\Products;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ProductFactory extends Factory
{
protected $model = Products::class;
public function definition(): array
{
return [
'name' => $this->faker->unique()->userName()
];
}
}
And add HasFactory Trait to your model:
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Products extends Model {
use HasFactory;
}
you can also use it like this
Products::factory()->count(5)->make();
I just uncommented these lines in app.php file
$app->withFacades();
$app->withEloquent();
Apparently Laravel 8 removed the 'factory' helper, and it seems Lumen followed that path without updating documentation;
#Faesal Answer is the correct way to do it these days;
remember to add use HasFactory; to your Model.
I am new to Laravel and after I added a new directory to the controllers viz, Admin, I updated the namespace also updated routes but somehow, I am getting a fatal error exception. Please help me figure out the problem
App->Http->Controllers->Admin
<?php
namespace App\Http\Controllers\Admin;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
routes.php
Route::get('/admin', 'Admin\AdminController#index');
snapshot of directory structure
try
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Note the "use" statement. I suspect that Laravel is looking for the Controller class, which this controller extends, but is unable to find it because of that missing statement.
I have created a simple controller and define a function. But when i run this it returns an error that controller does not exist.
In my web.php assign a route.
<?php
Route::get('/', function () { return view('front.welcome'); });
Route::get('plan','PlanController#PlanActivity')->name('plan');
On otherside in my controller my code:
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
class PlanController extends Controller {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}
This controller created on App\Http\Controllers\Front - on front folder
Error :
ReflectionException (-1)
Class App\Http\Controllers\PlanController does not exist
Add Front part to:
Route::get('plan', 'Front\PlanController#PlanActivity')->name('plan');
Also, change the top of the controller to:
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
And run composer du.
From the docs:
By default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.
First when defining route, make sure to use the correct path for the controller. the correct is:
Route::get('plan','Front/PlanController#PlanActivity')->name('plan');
Second you have imported Controller Class as BaseController. so you should extends BaseController not Controller:
class PlanController extends BaseController {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}
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'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
}