Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mac03733's avatar

use named route and asset in controller

i am trying to get the image and also set a named route in my controller

     foreach($users as $user){
             $output .= ' 
              <tr>
                 <td >'.$user->id.'</td>
                <td>
                <img src="{{ asset("storage/images/avatar5.png") }}" > //here ?? <-------------
                </td>
                <td>'.$user->name.'</td>                      
                <td>
                <a href="{{route("admin.viewUser",["id"=>'. $user->id .'])}}" class="btn  d-inline">                 
                </a> //here ?? <-------------
                </td>
              </tr>
               ';
            }
        
0 likes
7 replies
florianhusquinet's avatar
Level 4

You are using blade tags inside your controller which is not valid. You should use the regular php syntax instead, just like you are doing inside the td. The {{ $variable }} syntax is just sugar code for .

<img src=" .  asset('storage/images/avatar5.png') . ">
<a href=" . route('admin.viewUser', ['id' => $user->id]) . " class="btn d-inline"></a>
Cruorzy's avatar
<a href="{{ route('admin.viewUser', $user->id) }}" class="btn d-inline"></a> // Does not have to pass a array when its only 1 value

Assets require a bit of digging also the php artisan storage:link has to be used

mac03733's avatar

@Cruorzy i already used php artisan storage:link .. i was using the code in my blade template and it works ..but now i needed to move it to my controller

Cruorzy's avatar

Oh wouw, you are outpussing html from the controller? Would not have noticed that..

Michael112's avatar
 foreach($users as $user){
         $output .= ' 
          <tr>
             <td >'.$user->id.'</td>
            <td>
            <img src="{{ asset("storage/images/avatar5.png") }}" > //here ?? <-------------
            </td>
            <td>'.$user->name.'</td>                      
            <td>
            <a href="{{route("admin.viewUser",["id"=>'. $user->id .'])}}" class="btn  d-inline">                 
            </a> //here ?? <-------------
            </td>
          </tr>
           ';
        }
    

Please or to participate in this conversation.