How do I use helper functions in Lumen? - laravel

$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');

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

Method Illuminate\Support\Str::of does not exist

I want to get data from API. But when I put the code below.
public function FUNCTION_NAME()
{
$data['title']='Title';
$data= Http::get('API_LINK')->json();
return view('user.PAGENAME', $data);
}
Then showing Method Illuminate\Support\Str::of does not exist.
How solved that
It may happen while upgrading laravel framework version of your project. Try clearing application cache, update composer, or queue:restart if it is used.
The error you are having doesn't seem to relate to the snippet you have provided.
It is complaining that the laravel sting helper Str does not have an of() method available.
You can find all of the available methods for this class in the documentation:
https://laravel.com/docs/7.x/helpers

"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

location of helper function in Laravel 5

I am using Laravel 5 and new in this Platform. I studied about URL:asset In the earlier version of PHP, we could find the helper function in Auto Load or config files.
Do the helper function list exists somewhere in the Framework directory where if we want to change something in the helper function according to our need.
The helper functions are present in
/vendor/laravel/framework/source/illuminate/Foundation/helpers.php
You can find your helper function inside app.php as facades are registered on app.php and service container would let you resolve the class anywhere you want by use shorthandname or registered name;

Resources