Home > Net >  How to return to the DOM a string variable from controller that contains an html image tag with asse
How to return to the DOM a string variable from controller that contains an html image tag with asse

Time:04-05

I am facing an issue with Laravel 9. In my controller, I have created a variable which contains inside quotes the html code to be returned to the view.

I have a problem to determine where I should use single quotes and double quotes or backticks so that the {{asset()}} in the image src is compiled properly.

for now my image url looks like this in the console : http://127.0.0.1:8000/{{ asset(storage/upload/test2-png.png) }}

Here is the error in the console:

GET http://127.0.0.1:8000/{{ asset(storage/upload/test2-png.png) }} 404 (Not Found)

Here is the variable I am talking about:

$celebrityDetailsContent = `
                @if(isset($celebrityClicked))
                    <div id="celebrity_text_and_picture_container" >
                        <img src="{{ asset('storage/upload/'.{$celebrityClicked->image}) }}"  alt="Celebrity photo" title="Celebrity photo">
                        <p >{$celebrityClicked->firstname} {$celebrityClicked->lastname}</p>
                        <p >{$celebrityClicked->description}</p>
                    </div>
                @else 
                    <h2 >Click on a celebrity to display details</p>
                @endif
            `;

            return $celebrityDetailsContent;

Thanks for your help.

CodePudding user response:

Don't use {{ }} in your controller. It only works in your blade files. Try this:

if (isset($celebrityClicked)) {
    $celebrityDetailsContent = '
                <div id="celebrity_text_and_picture_container" >
                    <img src="' . asset('storage/upload/' . $celebrityClicked->image) . '"  alt="Celebrity photo" title="Celebrity photo">
                    <p >' . $celebrityClicked->firstname . ' ' . $celebrityClicked->lastname . '</p>
                    <p >' . $celebrityClicked->description . '</p>
                </div>';
} else {
    $celebrityDetailsContent = '<h2 >Click on a celebrity to display details</p>';
}

return $celebrityDetailsContent;
  • Related