使用softDeletes()更新外键

时间:2016-08-16 14:50:24

标签: laravel-5.2

您好我一直在寻找类似的解决方案但无法找到任何地方。我甚至都不知道是否有。我要做的是在我的某个模型上执行softDelete,并希望使用执行操作的用户的user_id更新模型中的id。我尝试过使用associate(),它不会抛出任何异常但不起作用。我的意思是删除工作正常,但id没有更新

这是我尝试过的事情

public function postDelete(Request $request){
   $appointment = \App\Models\emp_appointment::findOrFail($request->appid);
   $user = $request->user();
   $appointment->user()->associate($user);
   $appointment->delete();
}

这是我的预约模式

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class emp_appointment extends Model{
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    public function employee(){
        return $this->belongsTo('App\Models\employee','emp_id');
    }

    public function user(){
        return $this->belongsTo('App\Models\tb_login','userid');
    }
}

PS:模型没有问题,任何地方都没有错误。只有问题是userid没有在约会表中更新。

1 个答案:

答案 0 :(得分:0)

似乎删除方法没有保存您的更改(关联)

先尝试保存然后删除

public function postDelete(Request $request){
   $appointment = \App\Models\emp_appointment::findOrFail($request->appid);
   $user = $request->user();
   $appointment->user()->associate($user);
   $appointment->save();
   $appointment->delete();
}

修改

public function postDelete(Request $request){
   $appointment = \App\Models\emp_appointment::findOrFail($request->appid);
   $user = $request->user();
   $appointment->user()->associate($user);
   $appointment->deleted_at= date('Y-m-d H:i:s');
   $appointment->save();
}