Laravel 5.5 custom helper file Class become doesn't exists

1.3k views Asked by At

This is my code:

//helpers.php
if (!function_exists('image_helper')) {
    function image_helper($file = NULL, $path = NULL)
    {
        $image = app('image_helper');

        if (!is_null($file) && is_null($path)) {
            return $image->generateTmp($file);
        }

        return $image;
    }
}

// ImageHelper.php
<?php

namespace App\Helpers;

use Illuminate\Http\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Spatie\ImageOptimizer\OptimizerChainFactory;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ImageHelper
{
    private $fileName, $sizes;

    function __construct()
    {
        $this->fileName = md5(time() . openssl_random_pseudo_bytes(16));
    }

    public function get($query, $path, $type)
    {
        $arr = [];
        $sizes = getImageSizes($type);

        $picture = $query->picture()->first();

        if ($picture) {
            foreach ($sizes as $key => $size) {
                $url = filter_var($picture->file, FILTER_VALIDATE_URL) ?
                    $picture->file :
                    Storage::url($path . $key . '/' . $picture->file);

                $arr = array_add($arr, $key, $url);
            }
        }

        return $arr;
    }
?>

I've already add at my composer.json:

"autoload": {
"classmap": [
  "database/seeds",
  "database/factories",
  "app/Helpers/helpers.php"
],
"psr-4": {
  "App\\": "app/"
},
"files": [
  "app/Helpers/helpers.php"
]
},

When I run this code: image_helper()->get($query, '/images');

I got ReflectionException (-1) Class image_helper does not exist

Already do composer dump-autoload, php artisan optimize it still the same.

Previously on Laravel 5.4 it works perfectly.

Any solution?

1

There are 1 answers

1
ssuhat On

Problem is on this line in your helper function:

$image = app('image_helper');

Change it to this:

$image = app('App\Helpers\ImageHelper');

And now its working