Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中复制_Php_Laravel 5 - Fatal编程技术网

如何在php中复制

如何在php中复制,php,laravel-5,Php,Laravel 5,我正在尝试创建一个功能,它将复制/克隆/复制一个产品,包括它的所有属性和装运选项 但是,我成功复制了该产品,但未复制装运选项。见下面我的代码 任何帮助都将不胜感激 谢谢 public function CreateProductPost(Request $request){ if (Auth::user()->vendor == false) { return redirect()->route('profile'); } if ($request->nam

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

但是,我成功复制了该产品,但未复制装运选项。见下面我的代码

任何帮助都将不胜感激

谢谢

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');
}
任何帮助都将不胜感激


谢谢

您需要复制您的
产品
发货选项
型号,因此请使用以下逻辑:

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');
}
$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();
}
注意,您需要在
产品
发货选项
之间建立关系,否则您需要手动查询它们:

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

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

您需要复制您的
产品
发货选项
模型,因此请使用以下逻辑:

$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();
}
注意,您需要在
产品
发货选项
之间建立关系,否则您需要手动查询它们:

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

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

也许这会对您有所帮助:我为可怜的家伙感到抱歉,他必须填写该表单,获得一条错误消息,再填写一些,再获得一条错误消息,等等。大约3轮之后,我已经完成了一个网站。。。只是说说而已。找到所有错误并一次显示出来更有效(无论是代码方面还是用户方面)。使用适当的验证。。。您可以在一次检查中验证您的每一个输入,并在一次调用中返回所有相关错误,而不是一次又一次地返回第一个错误,直到正确为止。详细信息请参阅。也许这会帮助你:我为这个可怜的家伙感到抱歉,他必须填写表格,收到一条错误信息,再填写一些,再收到一条错误信息,等等。大约3轮之后,我就完成了一个网站。。。只是说说而已。找到所有错误并一次显示出来更有效(无论是代码方面还是用户方面)。使用适当的验证。。。您可以在一次检查中验证您的每一个输入,并在一次调用中返回所有相关错误,而不是一次又一次地返回第一个错误,直到正确为止。有关详细信息,请参阅。这是一个神奇的解决方案。非常感谢。顺便问一下,我该如何实现这一点<代码>$oldShippingOptions=ShippingOption::where(“product_id)”,“=”,$product->id)->get();foreach($shippingoptions作为$shippingOption){…}与上面的
foreach
逻辑相同。从逻辑上讲,
$product->shippingOptions
$oldShippingOptions
将包含与
ShippingOption
模型相同的
集合。这是一个神奇的解决方案。非常感谢。顺便问一下,我该如何实现这一点<代码>$oldShippingOptions=ShippingOption::where(“product_id)”,“=”,$product->id)->get();foreach($shippingoptions作为$shippingOption){…}
与上面的
foreach
逻辑相同。从逻辑上讲,
$product->shippingOptions
$oldShippingOptions
将包含与
ShippingOption
型号相同的
集合。