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

COACHTHEM's avatar

Best Practice for passing data to view.

Here is an example of displaying profile pics of list of users done in two different ways. I want to know what would be the good practice in terms of passing data to view. should I be looping the raw data in the controller method to fetch the image or do it while looping with in the view.

Way 1

public function controllerMethodHere(){
    
    $RawUsers = User::select([
        'id',
        'pic'
    ])
    ->where([
        ['status', 'A']
    ])
    ->get()
    ->toArray();

   $Users = [];
   foreach($RawUsers as $RawUser){

       $profile_pic = config('app.USER_DEFAULT_PIC');
       if(
           strlen(trim($RawUser['pic']))
           && \Storage::exists( config('app.USER_CDN_PATH').$RawUser['pic'] )
       ){
           $profile_pic = \Storage::url( config('app.USER_CDN_PATH').$RawUser['pic'] );
       }

       $RawUser['profile_pic'] = $profile_pic;
       $Users[] = $RawUser;

   }

   return view('user.list',[
       'Users'=>$Users
   ]);    

}


user/view.blade.php
<div>
@foreach ($Users as $User)
    <img src="'{{ $User['profile_pic'] }}'">
@endforeach
</div>

Way 2.

public function controllerMethodHere(){
    
    $Users = User::select([
        'id',
        'pic'
    ])
    ->where([
        ['status', 'A']
    ])
    ->get()
    ->toArray();

   return view('user.list',[
       'Users'=>$Users
   ]);    

}


user/list.blade.php
<div>

<div>
@foreach ($Users as $User)
    @php 
           $profile_pic = config('app.USER_DEFAULT_PIC');
           if(
               strlen(trim($User['pic']))
               && \Storage::exists( config('app.USER_CDN_PATH').$User['pic'] )
           ){
               $profile_pic = \Storage::url( config('app.USER_CDN_PATH').$User['pic'] );
           }            
    @endphp
    <img src="'{{ $profile_pic }}'">
@endforeach
0 likes
6 replies
lostdreamer_nl's avatar

Give your User model the following method:

public function getProfilePicAttribute() 
{
       $profile_pic = config('app.USER_DEFAULT_PIC');
       if( strlen(trim($this->pic))
           && \Storage::exists( config('app.USER_CDN_PATH').$this->pic )
       ){
           return \Storage::url( config('app.USER_CDN_PATH').$this->pic );
       }
    // you could also return a default pic for people without profile picture
       return false;
}

In your controller, stop using the toArray() on the collection.

public function controllerMethodHere(){
    
    $users = User::where('status', 'A')->get();

   return view('user.list', compact('users'));    

}

See how small that Controller can be?

Now for the view:

@foreach ($users as $user)
    @if($user->profile_pic)
        <img src="'{{ $user->profile_pic }}'">
    @endif
@endforeach

Nice and clean.....

Have a look at https://laravel.com/docs/5.6/eloquent-mutators for more info

Cronix's avatar

It's really wasteful timewise/memorywise to retrieve results, loop over them and stick them in a new array... Might be fine for a few users, but imagine thousands, or more.

Using an accessor avoids all of that and just provides a clean and efficient way to get a property off the main object, even if that property doesn't originally exist in the result collection.

Look at how much simpler the code is too.

lostdreamer_nl's avatar

Arrays are fine really, but if you want less logic in your views, you're better off passing models and collections in there and having accessors / mutators with the logic in there.

toArray() will also introduce some overhead because it will go over all attributes, checking for mutators etc. even though a lot of those fields are not needed.

Please or to participate in this conversation.