'Database Relationship Between Post and Post Files

I am trying to create an app store website. I am using dropzone chunks upload. The problem is That dropzone always send data first. I need Post_id as Foreign ID on (File Table) but dropzone sending data first before the post was created. How can I solve this? I want to send data together with submit button from Form. Or is There a better solution.

Here Is The PostController Code

//Post Start

  $post=new Post();
  $post->title=$request->title;
  $post->slug=Str::slug($request->title);
  $post->description=$request->description;
  $post->excerpt=Str::words($request->description, 30, '...');
  $post->user_id=Auth::user()->id;
  
  if($request->hasFile("cover_photo")){
      $file=$request->file("cover_photo");
      $newName=uniqid()."_cover_.".$file->extension();
      $file->storeAs("public/covers",$newName);

      $post->cover_photo=$newName;
  }
  $post->save();
 

if($request->hasFile("file")){

    //Start Merging
    $receiver=new FileReceiver('file',$request,HandlerFactory::classFromRequest($request));
    
    if(!$receiver->isUploaded()){
         return response()->json("File Not Uploaded");
    }

    $fileReceived=$receiver->receive();
    if($fileReceived->isFinished()){
        $file=$fileReceived->getFile();
        $fileName=$file->getClientOriginalName();

    //Start Uploadin In DB
        $media=new MediaLibrary();
        $media->file_name=$fileName;
        $media->post_id= //I Dont Know What to do here because Dropzone sending Request First
    //End DB 
        $file->storeAs("public/file",$fileName);
    
    //End Merge

    //Start Chunks Unlink
        if(unlink($file->getPathname())){
            return response("Chunks Uploded and Deleted Successfully");
        }

    //End Chunks Unlink
    }
}


return $request;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source