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
Related
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
I use VS Code, not use PHP Storm. Should I install 'laravel-ide-helper'?
You can require this package with composer using the following command:
composer require --dev barryvdh/laravel-ide-helper
Then if you are using Laravel versions older than 5.5, add the service provider to the providers array in config/app.php:
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
In Laravel, instead of adding the service provider in the config/app.php file, you can add the following code to your app/Providers/AppServiceProvider.php file, within the register() method:
public function register(){
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
This basically allow your application to load the Laravel IDE Helper on non-production environments.
It does help with code completion (automatically importing classes, showing methods, etc..). It works with VSCode the same as it does for PHPStorm as far as I can tell. The 'ide-helper-models' also appears to work the same. I'm unsure what the 'ide-helper:meta' does, but the docs only mention support for PHPStorm.
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
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');
$credentials = app_path();
Results in:
[Symfony\Component\Debug\Exception\FatalThrowableError] Call to
undefined function App\LtClasses\app_path()
But it's listed as a helper here: https://laravel.com/docs/5.3/helpers#method-app-path
Those are the Laravel docs, you don't have the same helpers available on Lumen, you can have a look at the helpers that the Lumen framework comes with at
/vendor/laravel/lumen-framework/src/helpers.php
Workarounds to achive what you need here could be
app()->path();
app('path');
base_path('app');