代码之家  ›  专栏  ›  技术社区  ›  scopeak

使用updateOrCreate方法更新一对多关系

  •  0
  • scopeak  · 技术社区  · 5 年前

    Tag 有一对多的关系 Product 标签 可以有很多 Products tag_id 数据库上的关系。

    在我的编辑视图中,我允许用户编辑标记 products

    每个 product 表单上的字段是从 request() request('title'),request('price') .

    $title[$key] 作为 request('title')

    我的下一个想法,是在 为了这个 tag updateOrCreate 基于请求数据。这里的问题是,没有办法检测 产品

    TagController-更新产品模型(一对多)

    foreach($tag->products as $key => $product){
    
      Product::updateOrCreate([
       'id'  => $product->id,
       ],
         [
           'title' => $title[$key],
           'price' => $price[$key],
           'detail' => $detail[$key],
           'order' => $order[$key],
           'tagX' => $tagX[$key],
           'tagY' => $tagY[$key],
           'thumb' => $img[$key],
       ]);
    }
    

    标签 更新,我已经设置了 if 对主标签有效的语句(尽管很混乱) img .

    //Update the tag collection
    if($request->hasFile('img')) {
      $tag->update([
        'name' => $name,
        'hashtag' => $hashtag,
        'img' => $imgPub,
      ]);
    } else{
      $tag->update([
        'name' => $name,
        'hashtag' => $hashtag,
      ]);
    }
    

    有没有更好的方法来确定 产品 根据请求更新了字段?

    产品 标签

    0 回复  |  直到 5 年前
        1
  •  2
  •   Udhav Sarvaiya Rodrigo Basilio    5 年前

    依我看,你走错了方向。标记关系应该是 多对多。

    你必须检查一下 sync() 多人关系中的拉沃尔方法。请检查: https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

    笔记:

    产品.php

      public function products(){
        return $this->belongsToMany( 'App\Product', 'product_tags', 'tag_id', 'product_id');
      }
    

    标记.php

       public function tags(){
         return $this->belongsToMany( 'App\Tag', 'product_tags', 'product_id', 'tag_id');
      }
    

    ProductController.php产品控制器

      public function update(Product $products,  Request $request){
        //update the product
        $products->update($request->all());
    
        //attach new tags to the product 
        $products->tags()->sync($request->input('tag_list'));
    
        return redirect('articles');
     }
    
        2
  •  0
  •   Yogesh Gupta    5 年前

    通常我在前端做类似的事情

    <div class="product">
       <!-- For Products that may be edited -->
       <input type="hidden" name="id" value={{$product->id??0}}>
       <input type="hidden" name="action" value="update">
       <input type="text" name="title" value={{$product->title}}>
          ...
    </div>
    <div class="product">
       <!-- For New Products -->
       <input type="hidden" name="id" value=0>
       <input type="hidden" name="action" value="add">
       <input type="text" name="title" value="New Title">
          ...
    </div>
    <div class="product">
       <!-- For Deleted Products -->
       <input type="hidden" name="id" value={{$product->id}}>
       <input type="hidden" name="action" value="delete">
       <input type="text" name="title" value="New Title">
          ...
    </div>
    

    [{'id':101,'title':'product 1','action':'update'},
    {'id':102,'title':'product 2','action':'update'},
    ... , 
    {'id':201,'action':'delete'}, 
    {'id':202, 'action':'delete'},
    ... ,
    {'id':0,'title':'new product 1','action':'add'}, 
    {'id':0,'title':'new product 2','action':'add'}]
    

    现在在后端,循环查看产品列表并检查操作。根据行动,采取必要的步骤。