laravellaravel-blade

How to add received data in blade to public_path() function in Laravel?


I have image names stored in a DB table. When going to a blade, I want to load an image using the data received in the blade using the compact() function. I tried this:

<img src="{{public_path('/assets/img/{{$img}}')}}" alt="">

So:

<img src="{{public_path('/assets/img/$img')}}" alt="">

Doesn't work. Tell me how to insert dynamic data into the image path?


Solution

  • First of all, there is the asset() function.

    Second, you just have to use string concatenation correctly:

    <img src="{{ asset('/assets/img/' . $img) }}" alt="">
    

    It is wise to save the full public path to the image, not just the image name, so you can use that path throughout the whole application, like when trying to get its dimensions, size, etc. With that, you can just {{ asset($uri) }}.