Friday, May 6, 2022

[FIXED] How do I show a picture on different pages using Laravel Route Prefix?

Issue

I have a image file. More than one pages is using this image file. Image is properly displayed in dashboard. Because this page not use Route Prefix. For example; Brand page is using Route Prefix. The picture is not displayed on this page.

Route::middleware(['auth'])->group(function () {
Route::get('dashboard', [homeAppController::class, 'dashboard'])->name('dashboard');

//web.php
Route::controller(brandController::class)->group(function () {
    Route::group(['prefix' => 'brand', 'as' => 'brand.'], function () {
        Route::get('/', 'index')->name('index');
        Route::get('/create', 'create')->name('create');
        Route::get('/store', 'store')->name('store');
        Route::get('/show/{id}', 'show')->name('show');
        Route::get('/edit/{id}', 'edit')->name('edit');
        Route::get('/update/{id}', 'update')->name('update');
        Route::get('/destroy/{id}', 'destroy')->name('destroy');
    });
});

//Dashboard
<img alt="" src="../assets/media/svg/brand-logos/{{ $data->logo }}"/>

//brand/show    
<img class="rounded-0" src="../assets/media/svg/brand-logos/{{ $brand->logo }}" alt=""/>

The picture is not displayed on this page. Because route started with brand prefix. For example; .../brand/assets/...


Solution

This is happening cause you are using relative paths, so it would be different on routes.

You can use full url like foo.com/bar.jpg or Simply just /bar.jpg (starting / is important)

or if you're on Laravel blade, you can also use

<img src="{{ asset('bar.jpg') }}" /> 


Answered By - Aniket Das
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.