Call to undefined function App\Http\Controllers\get_option() in laravel 8

2.1k Views Asked by At

I'm trying to convert an Laravel 5.7 project to 8, still debugging but I can't fix that spesific issue.

When I run that project I get "Call to undefined function App\Http\Controllers\get_option()" error.

I tried:

  • Change helpers.php location Providers to Controllers
  • Remove if clause in helpers.php file
  • Add "use App\Providers\helpers;" in Mainpage controller
  • Check Laravel 8 upgrading guide (https://laravel.com/docs/8.x/upgrade)

This is a source of error in App\Http\Controllers\MainpageController:

...
        $products_recommendations = Product::select('product.*')

            ->join('product_details', 'product_details.product_id', 'product.id')

            ->where('product_details.show_recommendations', 1)

            ->orderBy('update_date', 'desc')

            ->take(get_option('mainpage_list_product_count'))->get();
...

This is app/Providers/helpers.php file, it has get_option() function:

<?php
use App\Models\Option;
use Illuminate\Support\Facades\Cache;

if (! function_exists('get_option')) {
    function get_option($key) {
        //$allOptions = Cache::rememberForever('allOptions', function() {
        $minute = 60;
        $allOptions = Cache::remember('allOptions', $minute, function() {
            return Option::all();
        });
        
        return $allOptions->where('key', $key)->first()->variable;
    }
}

How can I fix that?

Note: I translated variable and function names into English for asking here.

1

There are 1 best solutions below

0
On BEST ANSWER

I added my helper again to my composer.json file:

    "autoload": {
    "files": [
        "app/helpers.php"
    ],

and enter composer dump-autoload. It worked.

That solution hadn't worked before, I had to delete and rewrite it in the composer.json file.

There is another error, but at least I fixed this.