Laravel 8 分别上传两张图片。在数据库中添加相同的图像名称

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

这是控制器代码:

 $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);

///这是添加到数据库:

       $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'),
        
       
        
    ]);

这是在查看 blade:

上传图片1 上传图片2

如果有人能提供帮助,我将不胜感激

您应该首先查看您在 Chrome、Firefox 或您使用的任何软件中的检查,并检查您的请求包含的内容,我的意思是如果您以单独的名称发送图像,例如: ... player1_Id: player2_Id: ... 我认为,当然是那样发送,因为您正在控制器上接收它。然后尝试在名称末尾使用日期时间保存它,例如:

public function obtainImage(Request $request){

$image1=request('player1_Id');

$this->manageImage($image1);

$image2=request('player2_Id');

$this->manageImage($image2);
}

public 函数 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;

}

其中 $ newFileName 是您需要保存到数据库中的内容

否则你可以在保存到数据库和比较名称之前执行 dd ($player1QID.'-'.$player2QID)