Php laravel-更新一对多多态关系

Php laravel-更新一对多多态关系,php,laravel-5.8,Php,Laravel 5.8,我在一对多多态关系中有三个表 ┌─────────────────┐ ┌─────────────────┐ ┌──────────────────┐ │ users │ │ phones │ │ departments │ ├─────────────────┤ ├─────────────────┤ ├──────────────────

我在一对多多态关系中有三个表

┌─────────────────┐          ┌─────────────────┐          ┌──────────────────┐
│ users           │          │ phones          │          │ departments      │
├─────────────────┤          ├─────────────────┤          ├──────────────────┤
| id              |          | id              |          | id               |
| name            |          | name            |          | name             |
└─────────────────┘          | phones_type     |          └──────────────────┘
                             | phones_id       |          
                             └─────────────────┘          
电话型号:

public function phones()
 {
   return $this->morphTo();
 }
用户模型:

public function phones()
 {
   return $this->morphMany('App\Phone', 'phones');
 }

部门模式:

public function phones()
 {
   return $this->morphMany('App\Phone', 'phones');
 }
现在我想更新电话号码,以便用户可以编辑/添加/删除号码。。我无法一步到位,所以我尝试先删除所有手机,然后添加新号码

我的代码: 部门总监:

public function update(UpdateDepartment $request, Department $department)
    {
        $department->phones()->delete();
        $department->name = $request['name'];
        $department->save();
        foreach ($request->phone as $value) {
            $phone = new Phone;
            $phone->name = $value;
            $ department->phones()->save($phone);
        }
    }
UpdateDepartment.php:

public function rules()
    {
        return [
            'name' => 'required|alpha_spaces|min:3|max:255|unique: department,name,'.$this-> department ->id,
            'phone'    => 'required|array|unique:phones,name,'
            'phone.*'  => 'required|distinct|unique:phones,name,',
        ];
    }
我有两个问题:

1-这样做对吗?有没有办法一步同步更新的电话号码


2-如果是这样的话。。。如果用户只是将电话号码添加到现有号码中,如何强制唯一规则忽略给定的ID?

1-您可以发送一些带有新电话号码的标识符(ID),这样您就可以知道修改的具体内容,例如放置一些
数据ID=“{{{$phone->ID}”
到电话字段,并使用javascript生成请求,以生成如下内容:

"phones":
  {
    "1"(your phone id):"+123456789",
    "2":"+123654978",
  }
public function update(UpdateDepartment $request, Department $department)
{
    $department->name = $request['name'];
    foreach ($request->phones as $id => $value) {
        $department->phones->transform(function($item) use ($id, $value){
            if($item->id == $id) {
                $item->name = $value;
                $item->save(); //if You using laravel 5 You need to savve each phone model separately
            }
        });
    }

    $department->push(); //if You using laravel 6.x, this will save model with all changed/created relations
}
然后像这样迭代请求数据:

"phones":
  {
    "1"(your phone id):"+123456789",
    "2":"+123654978",
  }
public function update(UpdateDepartment $request, Department $department)
{
    $department->name = $request['name'];
    foreach ($request->phones as $id => $value) {
        $department->phones->transform(function($item) use ($id, $value){
            if($item->id == $id) {
                $item->name = $value;
                $item->save(); //if You using laravel 5 You need to savve each phone model separately
            }
        });
    }

    $department->push(); //if You using laravel 6.x, this will save model with all changed/created relations
}

UPD 但我建议将电话号码作为
json
存储在用户和部门的单独字段中,这样就可以简单地将模型中的旧号码集替换为请求中的新号码集,而无需对数据库进行不必要的查询