How to use Laravel Debugbar in controller? - laravel-5

I got an error Class 'App\Http\Controllers\Debugbar' not found after trying to use in controller action Debugbar::error('Error!');
LARAVEL DEBUGBAR WAS INSTALED IN THIS WAY:
in console composer require barryvdh/laravel-debugbar
in config/app.php adding Barryvdh\Debugbar\ServiceProvider::class and 'Debugbar' => Barryvdh\Debugbar\Facade::class,
Laravel 5.3
Debugbar is working in the website - I can see it at the bottom even after this error. But how to fix this critical error message?

Either explicitly use the Facade or use Debugbar from the root namespace.
\Debugbar::error('hi');
// OR
use \Debugbar;
Debugbar::error('hi');
// OR
use Barryvdh\Debugbar\Facade as Debugbar;
Debugbar::error('hi');

Related

Call to undefined function factory()

Environment: Laravel Framework Lumen (8.2.2) (Laravel Components ^8.0)
When I run
$blogs = factory('App\Blog', 2)->create();
in BlogsControllerTest.php, it shows
Call to undefined function factory()
As Laravel's upgrade guide says the Model factory was changed.
The new way is like this App\Models\Blog::factory()->count(3)->create();
To use old version referrer to documentation.
However, to ease the upgrade process, a new laravel/legacy-factories package has been created to continue using your existing factories with Laravel 8.x
To install it use composer composer require laravel/legacy-factories
Answering my own question in case it helps somebody.
It works this way too.
$blogs = BlogFactory::new()->count(2)->create();
ref: laravel.com/docs/8.x/database-testing#connecting-factories-and-models

"Call to undefined function str_slug()" in Laravel 6.0

I've upgraded my laravel 5.8 project to 6.0. It has upgraded successfully but when I'm trying to run the project or installing another package to my project it is giving me error named as "Call to undefined function str_slug()" in session.php. I don't know why....
Call to undefined function str_slug()
If you have gone through the upgrade guide then you must know that
String and Array Helpers have been removed from Core Framework
So if if You need to still use the helper install the package
composer require laravel/helpers
and all the helpers are moved to this package
String and Array helpers are removed from laravel 6.0 Core Framework
https://laravel.com/docs/6.0/upgrade#helpers
So if You need to still use the helper install the package
composer require laravel/helpers
Or you can use by Laravel facade
use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5 Framework', '-');
Personal I hard to do the following on Laravel 6
on the app Controllers add this use Illuminate\Support\Str; then
something like this 'slug' => Str::slug($request->title)
There are two options to resolve the issue of call to undefined function str_slug().
1.You should run the command composer require laravel/helpers
Or another option is when you don't want to install packages then this below solution is the easy way to solve your issue and it is the best way.
2.You can use facades class
use Illuminate\Support\Str;
public function index(Request $request)
{
$slug = Str::slug($request->name);
}
$post = Post::create([
'slug' => S t r::slug($request->title),
here we go
composer require laravel/helpers
php artisan optimize:clear

Datattables Laravel 5.4 - FatalThrowableError Call to undefined function App\Http\Controllers\Datatables()

I'm using datatables with laravel 5.4, but when I use the datatables()->query():
in providers: Yajra\Datatables\DatatablesServiceProvider::class,
in aliases: 'Datatables' => Yajra\Datatables\Facades\Datatables::class,
My code:
return Datatables()->query( /**query**/ )
return this error in my console:
FatalThrowableError
App\Http\Controllers\Datatables()
I've tried datatables() and Datatables()
Someone with the same problem?
This error happens because Laravel cannot find your package Datatables.
Laravel in controllers, if you are not mapping your classes correctly will always seek the called classes at this point App\Http\Controllers.
Just put use DataTables; or use \Yajra\Datatables\Datatables; at the start of your controller and it shall be fixed. documentation
When you install any package through composer, composer uses namespace to find the package and route functions and classes to it.
you can read more about the namespaces from php.
and you can check this tutorial for namespaces.
Also, Laravel follows the PSR 4 when it comes to autoloading, you can check it from here

What is the proper way to get ready a 3rd party package in Laravel 5 (5.4)?

I try to migrate an existing project from laravel 4 to laravel 5.
For that I installed a fresh laravel project and import code in it.
I installed a required package in laravel 5:
composer require jenssegers/agent
When I call Agent class to use, laravel gives following error:
Class 'App\Http\Controllers\Agent' not found
What is "use ..." line to add at the top of controller? Or any other solutions?
Not: use Agent; results in Class 'Agent' not found error
Since the Agent is a facade, you should use full namespace:
$agent = \Agent::....;
Or add use clause to the top of your controller:
use Agent;
You need to add facades in app/config/app.php
Laravel (optional)
Add the service provider in app/config/app.php:
'Jenssegers\Agent\AgentServiceProvider',
And add the Agent alias to app/config/app.php:
'Agent' => 'Jenssegers\Agent\Facades\Agent',
Source: https://github.com/jenssegers/agent

Laravel JWTAuth Custom User Model

I'm developing web using Laravel and JWTAuth with default user model at
App\User
Now i'm changin the path of my models to
App\Models
App\Models\User
and i change the jwt config to this
'user' => 'App\Models\User',
but when i try to access my login page, it's show error like this
FatalErrorException in EloquentUserProvider.php line 126:
Class 'App\User' not found
what happening with this? i've changing the setting to App\Models but it's still load to App\ directory?
I re-serve the php artisan, and do composer dump-autoload
It's the same error.
I've resolved my error,
there's one config called auth.php that must updated too
Thanks

Resources