Search code examples
laravel-8image-upload

Laravel 8 uploading two images seperately. added same image names in database


This is the controller code:

 $player1QID = time().'.'.$request->player1_Id->extension(); 
    $images1= $request->player1_Id->move(public_path('images'), $player1QID);
   
    $player2QID = time().'.'.$request->player2_Id->extension();  
     
    $images2= $request->player2_Id->move(public_path('images'), $player2QID);

///this is adding to database:

       $registeredusers = Registrations::create([
        'tournament_id' => $request->input('tournament_id'),
         'player1_name' => $request->input('player1_name'),
         'player1_email' => $request->input('player1_email'),
         'player1_Id' => $player1QID,
         'player1_gender' => $request->player1_gender,
         'player1_phone' => $request->input('player1_phone'),
         'player2_name' => $request->input('player2_name'),
          'player2_email' => $request->input('player2_email'),
          'player2_Id' => $player2QID,
         'player2_gender' => $request->player2_gender,
         'player2_phone' => $request->input('player2_phone'),
          'category' => $request->category,
          'status' => $request->input('status'),
        
       
        
    ]);

This is in view blade:

Upload image1 Upload image2

Really appreciate if someone can help


Solution

  • you should first look at your inspection in Chrome, Firefox or whatever you are using, and check what your request contains, I mean if you are sending the images in separate names like: ... player1_Id: player2_Id: ... i think, of course is sending like that because you are receiving it on your controller. Then try save it with datetime at end of name like:

    public function obtainImage(Request $request){
    
    $image1=request('player1_Id');
    
    $this->manageImage($image1);
    
    $image2=request('player2_Id');
    
    $this->manageImage($image2);
    }
    

    public function manageImage($image){ $fileImageNameExtencion=$image->getClientOriginalName();

      $fileName=pathInfo($fileImageNameExtencion, PATHINFO_FILENAME);
    
      $fileExtencion=$image->getClientOriginalExtension();
    
      $newFileName=$fileName."_".time().".".$fileExtencion;
    
      $saveAs=$image->storeAs('public/images',$newFileName);
      return $newFileName;
    

    }

    where $ newFileName is what you need to save to your database

    otherwise you can do a dd ($ player1QID. '-'. $ player2QID) before saving to database and comparing names