如何在php中复制

时间:2019-03-26 19:28:31

标签: php laravel-5

我正在尝试创建一个功能,该功能将复制/克隆/复制包括其所有属性和运输选项的产品。

但是,我成功地复制了产品,但没有复制运送选项。请参阅下面的代码;

任何帮助将不胜感激

谢谢

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');
    }

任何帮助将不胜感激

谢谢

1 个答案:

答案 0 :(得分:0)

您需要同时复制ProductShippingOption模型,因此请使用以下逻辑:

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

请注意,您需要在ProductShippingOption之间建立联系,否则,您将需要手动查询它们:

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

->replicate()方法不会克隆所有相关记录,因为这可能不是预期的要求,因此您需要手动进行。