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

JackD's avatar

@vipin93 it's ok now i already figured it out

@lalitesh THANK YOU! i figured it out now, i play around with the code you replied and got what i should display :D

JackD's avatar

@vipin93 right now i want them to be list randomly what i have now in my controller is this

$users = User::orderByRaw("RAND()")->get();

is there any way around to do it?

jekinney's avatar

@dinis Just to do a quick check to see if the row is available or not.

Very basic example:

$imageExists = ImageModel::where('user_id', $userId)->first();

if($imageExists)
{
    $imageExists->image_path = $newImagePath;
    $imageExists->save();
} else {
    ImageModel::create(); //just as you have already set up
}
JackD's avatar

@jekinney i came up with the code below, is it acceptable?

`

    <?php foreach ($users as $user) {

                            if(isset($user->photo) && (!$user->photo->profileimage)){
                                echo "<li><a href=/".$user->id."><h1><img src=". $user->photo->profileimage .">". $user->name ."</h1></a></li>";    
                            }
                            else
                            {
                                if(isset($user->photo->profileimage)){
                                    echo "<li><a href=/".$user->id."><h1><img src=". $user->photo->profileimage .">". $user->name ."</h1></a></li>";    
                                }
                                else {
                                    echo "<li><a href=/".$user->id."><h1><img src='/hphotos/xpf1/noprofile.jpg'>".$user->name."</h1></a></li>";
                                }       
                            }
                        }
                    ?>
                </ul>   
    
lalitesh's avatar

@dinis, that code was for the view's blade template. Could you share your controller and view code if the issue was not resolved yet.

JackD's avatar

@lalitesh i can already display them correctly with name and designated image, but when the user upload a new image it still display the first image that has been uploaded, it should display the latest image that the user uploaded

i tried to use Photo::latest('created_at')->get(); i still can't get the latest uploaded image by the user, it still display the first uploaded image

` public function index() { $photos = Photo::latest('created_at')->get(); $users = User::orderByRaw("RAND()")->get(); $homepage = view('profile',compact("users","photos"));

    return $homepage;
}
lalitesh's avatar

@dinis This issue could be because of the following reasons:

  • Old image is not deleted from the media table
  • Old image is not deleted from the file server
  • Some kind of cache is playing
JackD's avatar

@lalitesh yeah deleting the old image from my public folder wasn't solve yet, any idea what code should i use so that when a user uploaded a new image the old image will be deleted form database and public folder?

lalitesh's avatar

@dinis they cannot be removed on its down. If you are using a pivot table or other relationship, try using detch method as described at http://laravel.com/docs/4.2/eloquent, Also, in the edit/ update controller action you would need to write the code to delete the image.

See below a code snippet that I had used in a category CRUD

if (Input::get('image_name')) {
            $media = new Asset;
            
            /*delete old image from DB and filesystem */
            foreach($category->assets as $asset){
                $destinationPath = Config::get('syntara::config.category-image-path');      
                $imgPath = public_path().'/'.$destinationPath.'/'.$asset->name;
                
                if(File::exists($imgPath)){
                    File::delete($imgPath);
                }

            }
            $category->assets()->delete($media);
            $media->name = Input::get('image_name');            
            $category->assets()->save($media);
        }

JackD's avatar

@lalitesh the problem is that i use uniqid() for naming uploaded images, do you have a code something like this

delete all from photos table where user_id = the auth id

bobbybouwmann's avatar

You need to update the database as well. So when you upload a new image you update the image attribute in the user model. You also delete the image that was used before ;)

JackD's avatar

@blackbird @lalitesh i came up with this code but the problem is that it delete all the photos of all user in the database except for the latest uploaded image. I also tried to use Photo::find(Auth::id())->get() but i got and error message "Call to a member function get() on null"

` $photos = Photo::latest('created_at')->get(); if(! $photos->isEmpty()) { foreach ($photos as $photo) { $photo->delete(); } }

bobbybouwmann's avatar

Find already returns an object so you don't have to use get any more ;)

JackD's avatar

@blackbird i came up with this code is that ok that i used "where"? or is there any way around easier to do that "where" query?

`$photos = Photo::where('user_id','=',Auth::id())->get(); if(! $photos->isEmpty()) { foreach ($photos as $photo) { $photo->delete(); } }

Previous

Please or to participate in this conversation.