您好我一直在寻找类似的解决方案但无法找到任何地方。我甚至都不知道是否有。我要做的是在我的某个模型上执行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
没有在约会表中更新。
答案 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();
}