Skip to content
Advertisement

Laravel Storage::disk(‘public_path’)->files() returning wrong path

In the following route, inspecting the image paths that this method returns, they all have “/categories/” at the start but this is not right, the actual path should be /public/images/categories/gallery/1 not /public/categories/images/categories/gallery/1

Route::get('/categories/{group:id}', function (Group $group) {
    $images = Storage::disk('public_path')->files('images/categories/gallery/' . $group->id);
    return view('categories.index', ['group' => $group], compact('images'));
})->name('categories');

The following is an example of a route where it actually gets the correct paths

Route::get('/news', function () {
    $categories = Category::get();
    $images = Storage::disk('public_path')->files('images/news/gallery');
    return view('news', ['categories' => $categories], compact('images'));
})->name('news');

So my question is, why is it in the previous route, it returns the incorrect path with /categories/ attached to the start but not in the one above, also how do I get the correct path in the first code example posted.

I tried changing the route to Route::get('/{group:id}', function ... etc and it works but I won’t be able to use that because the url subdir will just be a number.

I also tried adding a forward slash to the path in the files() method so it was ->files('/images/categories/gallery/1'); but it still appends /categories/ to the start.

This is the config for the method in filesystems.php

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        'public_path' => [
            'driver' => 'local',
            'root' => public_path(''),
        ],
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],

],

Advertisement

Answer

The problem was not in my php but in the vue component I was using which failed to place a forward slash at the start of the src path so I changed

<source media="(min-width: 768px)" :srcset="`${image}`">

to

<source media="(min-width: 768px)" :srcset="`/${image}`">

and it works.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement