使用 Laravel 5.5 在 sql 更新请求上重复 "deleted_at"

duplicate "deleted_at" on sql update request with Laravel 5.5

我在控制器中创建了一个销毁方法:

public function destroy($id)
{
    $sector = Sector::findOrFail($id);
    // on update lang_sector pour chaque id
    $sector_ids = $sector->langs()->allRelatedIds();
    foreach ($sector_ids as $id){
        $sector->langs()->updateExistingPivot($id, ['lang_sector.deleted_at' => Carbon::now(), 'lang_sector.updated_at' => Carbon::now()]);
    }
    $sector->valuechains()->update(
        [
            'valuechains.deleted_at' => Carbon::now(),
            'valuechains.updated_at' => Carbon::now()
        ]
    );

    Sector::where('id', $id)->delete();
}

在我的模型中:

protected $table = "sectors";
protected $fillable = ['admin_id'];
protected $dates = [
    'created_at',
    'updated_at',
    'deleted_at'
];
public function langs() {
    return $this->belongsToMany('App\Lang')
        ->withPivot(
            'sectname',
            'sectshortname',
            'sectdescription',
            'sectshortdescription'
        )
        ->withTimestamps();
}

public function valuechains()
{
    return $this->hasMany('App\Valuechain');
}
public function segments()
{
    return $this->hasManyThrough('App\Segment', 'App\Valuechain');
}
public function keyneeds()
{
    return $this->hasManyThrough('App\Keyneed', 'App\Segment', 'App\Valuechain');
}

当我销毁我的条目时,我遇到了 sql 请求的问题:

更新valuechains设置valuechains.deleted_at = '2018-05-09 16:10:32', valuechains.updated_at = ' 2018-05-09 16:10:32', updated_at = '2018-05-09 16:10:32' 其中 valuechains.sector_id = '2' 和 valuechains.sector_id 不为空

没有任何特殊原因出现 updated_at ...

发生这种情况是因为您明确要求在此行中设置 updated_at 列:

'valuechains.updated_at' => Carbon::now()

Laravel 在更新期间自动设置 updated_at,因此您不需要自己明确更新它。

您应该可以删除该明确的行,并且 updated_at 仍会更新。

Laravel 应该足够聪明以检测重复的列更新,可惜它不是。