dongweiben5229 2019-03-26 19:28
浏览 28
已采纳

如何在php中复制

I'm trying to create a function that will replicate/clone/duplicate a product including all it's properties and it's shipping options.

However, I succeeded to duplicate the product but the shipping options are not replicated. See my codes below;

Any help will be highly appreciated

Thanks

public function CreateProductPost(Request $request){
  if (Auth::user()->vendor == false) {
    return redirect()->route('profile');
  }
  if ($request->name == null) {
    session()->flash('errormessage','Product name is required');
    return redirect()->back()->withInput();
  }
  if (mb_strlen($request->name) > 60) {
    session()->flash('errormessage','Product name cannot be longer than 60 characters.');
    return redirect()->back()->withInput();
  }
  if ($request->category_id == null) {
    session()->flash('errormessage','Product category is required');
      $shippingoptions[] = $opt;
    }
  }
  $product = new Product;
  $product->name = $request->name;
  $product->uniqueid = random_int(10000, 99999);
  $product->category_id = $category->id;
  $product->description = $request->description;
  $product->refund_policy = $request->refund_policy;
  $product->fromc = $request->fromc;
  $product->tocount = $request->tocount;
  $product->price = $request->price;
  $product->currency = $request->currency;
  $product->inventory = $request->inventory;
  if ($request->image !== null) {
    $product->image = $request->image->store('uploads','public');
  }
  $product->buyout = 0;
  $product->fe = $fe;
  $product->seller_id = Auth::user()->id;
  $product->save();
  foreach ($shippingoptions as $opt) {
    $so = new ShippingOption();
    $so->product_id = $product->id;
    $so->desc = $opt['desc'];
    $so->days = $opt['days'];
    $so->price = $opt['price'];
    $so->save();
  }
  session()->flash('successmessage','Product successfully        created');
  return redirect()->route('products');
}
function DuplicateProductPost($uniqueid, Request $request){
      $product = Product::where('uniqueid',$uniqueid)->first();
      if ($product == null) {
        return redirect()->route('products');
      }
      if (Auth::user()->id !== $product->seller->id) {
        return redirect()->route('products');
      }
      $newProduct = $product->replicate();
      $newProduct->uniqueid = random_int(10000, 99999);
      $newProduct->save();
      session()->flash('successmessage','Product successfully duplicated');
      return redirect()->route('products');
    }

Any help will be highly appreciated

Thanks

  • 写回答

1条回答 默认 最新

  • dongshi2588 2019-03-26 19:43
    关注

    You need to replicate both your Product and ShippingOption models, so use the following logic:

    $product = Product::where('uniqueid',$uniqueid)->first();
    ...
    $newProduct = $product->replicate();
    $newProduct->uniqueid = random_int(10000, 99999);
    $newProduct->save();
    
    foreach($product->shippingOptions AS $shippingOption){
      $newShippingOption = $shippingOption->replicate();
      $newShippingOption->product_id = $newProduct->id;
      $newShippingOption->save();
    }
    

    Note, you need to have a relationship between Product and ShippingOption, otherwise you will need to manually query for them:

    $oldShippingOptions = ShippingOption::where("product_id", "=", $product->id)->get();
    foreach($oldShippingOptions AS $shippingOption){
      ...
    }
    

    The ->replicate() method does not clone all related records, as that might not be the intended requirement, so you need to do it manually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀