gpt4 book ai didi

php - Laravel 避免在子表中重复输入

转载 作者:行者123 更新时间:2023-12-04 10:04:35 25 4
gpt4 key购买 nike

我的产品有很多条码,当我进入产品编辑页面并添加/删除/编辑产品的条码时,它会再次保存条码(PS:我知道问题来自哪里,但不确定解决方案为了它)

逻辑

所以当我在产品编辑页面时,这是我期望发生的事情:

  • 如果当前条形码已更改,只需更新它们
  • 如果删除了任何当前条形码,则将它们从数据库中删除
  • 如果有任何新条码添加到列表中,请将其添加到数据库

  • Basically is sync() functionality that i'm looking for, but based on my model relationships i think using sync() is not an option.



    代码
    Product model
    public function barcodes()
    {
    return $this->hasMany(Barcode::class, 'product_id', 'id');
    }
    barcode model
    protected $fillable = [
    'product_id', 'serial_number',
    ];

    public function product()
    {
    return $this->belongsTo(Product::class);
    }
    Controller (update function)
    public function update(Request $request, $id)
    {
    $product = Product::find($id);
    $product->name = $request->input('name');
    if($product->save())
    {
    if(!empty($request->input('barcodes')))
    {
    foreach($request->input('barcodes') as $bb)
    {
    if(!empty($bb['serial_number'])){ // make sure no empty value saves
    $barcode = new Barcode;
    $barcode->product_id = $product->id;
    $barcode->serial_number = $bb['serial_number'];
    $barcode->save();
    }
    }
    }
    }
    }

    As I mentioned above I am aware where the issue comes from, but not sure about the solution for it and the issue of duplicate saving data is that I'm using $barcode = new Barcode;.



    额外的

    为了清楚起见,这是我向后端发送更新请求时我的数据的样子

    one



    为了实现我的愿望逻辑,我应该在代码中更改什么?

    最佳答案

    这就是我为一对多关系创建同步功能的方式:

    public function update(Request $request, $id)
    {
    $product = Product::find($id);
    $product->name = $request->input('name');
    if($product->save())
    {
    if(!empty($request->input('barcodes')))
    {
    $barcodeSync = []; // We will use this to save all the barcode id's that exist in the database.
    foreach($request->input('barcodes') as $bb)
    {
    if(!empty($bb['serial_number'])){
    $barcode = $product->barcodes()->updateOrCreate(
    ['id' => $bb['id'] ?? 0],
    ['serial_number' => $bb['serial_number']]
    ); // This will create a new barcode for the product if that barcode doesn't exist yet or update the existing barcode using it's id.
    array_push($barcodeSync, $barcode->id); // This will add the id to the array of existing barcodes for this product.
    }
    }
    $product->barcodes()->whereNotIn('id', $barcodeSync)->delete(); //Deletes all barcodes for this product that were not in the request.
    }
    }
    }

    这应该可以解决您的 3 个期望。
  • 这将更新与“id”和“product_id”匹配的任何条形码。 (请注意,如果 'serial_number 没有改变,laravel 实际上不会更新模型)。
  • 它将所有为产品创建或更新的条码保存在 $barcodeSync 数组中,以便您以后可以删除请求中不再存在的所有条码 $product->barcodes()->whereNotIn('id', $barcodeSync)->delete();
  • 它将创建任何传入请求的新条码,由于新条码在请求中没有“id”字段,它将查询 id 为 0(0 将不存在),这将触发创建新条码和将其分配给产品。 (为此,还要确保条形码模型的“id”字段不在 $fillable 数组中,因为每次创建新条形码时它都会尝试将 id 设置为 0,这会导致错误)

  • 希望这对你有帮助!

    关于php - Laravel 避免在子表中重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61649769/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com