JackD's avatar

displaying static image if profile image is null

im having problem when that when there is no image upload yet from the user the last else in my view statement don't show which serve as my default image for those who dont upload image yet.

note: the image which supposed to be shown is the primary picture of the user who is currently logged in

controller `$photos = Photo::latest('created_at')->get();

    $leftwings = Photo::where('user_id','=',Auth::id())->get();
    $users = User::orderByRaw("RAND()")->get();
    $homepage = view('profile',compact("users","photos","leftwings"));
    
    return $homepage;

view `@foreach($leftwings as $leftwing)

                        @if(isset($leftwing->photo) && (!$leftwing->profileimage))
                    

                            <img style="max-width:50px;max-height:50px;min-width:50px;min-height:50px;" src=<?php echo "/hleftwings/xpf1/" . Auth::user()->id ."/". $leftwing->profileimage; ?> />
                    
                            @else
                            
                                @if(isset($leftwing->profileimage))
                                
                                    
                                <img style="max-width:50px;max-height:50px;min-width:50px;min-height:50px;" src= <?php echo "/hleftwings/xpf1/" . Auth::user()->id ."/". $leftwing->profileimage; ?> />
                    
                                
                                @else
                            
                                    
                                <img style="max-width:50px;max-height:50px;min-width:50px;min-height:50px;" src="/hleftwings/xpf1/noprofile.jpg" />
                                
                                @endif

                        @endif
            
                @endforeach
0 likes
15 replies
JarekTkaczyk's avatar

@dinis Rephrase it, it's impossible to understand what problem you have. Apart from @if(isset($leftwing->photo) && (!$leftwing->profileimage)) being wrong, if you want to use $leftwing->profileimage few lines later.

JackD's avatar

@JarekTkaczyk in my controller i have this $leftwings = Photo::where('user_id','=',Auth::id())->get(); to get the logged in user if it has photo uploaded, since there is no record it should display the last @else in my view code which is my default static image

JackD's avatar

@JarekTkaczyk my $leftwings = Photo::where('user_id','=',Auth::id())->get(); is getting null maybe my foreach() dont work what should i do if my query got null value and i have to output default image if i got null from query?

JarekTkaczyk's avatar

Photo::where('user_id','=',Auth::id())->get(); doesn't return null - it returns a Collection object always. What do you want to achieve once again?

JackD's avatar

@JarekTkaczyk it doesn't get anything i think because the user doesn't have anything yet in my Photo table. what i want to achieve is to display default image when the user don't have any matched id from the Photo table user_id

sitesense's avatar

isset() will return true even if it's an empty string. You could change it like this to avoid the ambiguity:

@if(isset($leftwing->profileimage) && $leftwing->profileimage)
JackD's avatar

@sitesense what are the other alternative way to do it? instead of using isset?

JarekTkaczyk's avatar

@dinis You want to get user with photo(s) not photos directly. Show tables structure and I'll tell you what you need.

sitesense's avatar

@dinis as I showed you in the code example - isset() will check if the variable exists and this part && $leftwing->profileimage will ensure that it actually has a value.

JackD's avatar

@JarekTkaczyk

User table $table->increments('id'); $table->string('name');

Photo table $table->increments('id'); $table->text('photoFilename'); $table->text('profileimage'); $table->integer('user_id')->unsigned();

sitesense's avatar

@dinis Found some other problems in your view. Try this instead:

@foreach($leftwings as $leftwing)
            
    @if(isset($leftwing->profileimage) && $leftwing->profileimage)
        
    <img style="width:50px;height:50px;" src= <?php echo "/hleftwings/xpf1/" . Auth::user()->id ."/". $leftwing->profileimage; ?> />
    
    @else
        
    <img style="width:50px;height:50px;" src="/hleftwings/xpf1/noprofile.jpg" />
    
    @endif

@endforeach
3 likes
JackD's avatar

@sitesense thanks.. my problem within my view still exist that when my controller find nothing that matched to the logged in user table id in my photo table user_id, it doesn't show the default image.

JackD's avatar

@sitesense i already solved it using @if(is_null($leftwingnull)) above my foreach ,it's ok now thanks!

Please or to participate in this conversation.