user()) { throw new BizException('绑定失败,请稍后再试'); } $input = $request->validate([ 'cid' => ['bail', 'required', 'string', 'max:64'], 'type' => ['bail', 'string', 'max:1'], ]); try { DB::beginTransaction(); $filed = Arr::get($input, 'type', 'u').'_cid'; //查询目前有没有人已绑定这个cid, 有就解绑 UserCid::where($filed, $input['cid'])->where('user_id', '<>', $request->user()->id)->update([ $filed => '', ]); $request->user()->cid()->updateOrCreate([ 'user_id' => $request->user()->id, ], [ $filed=>$input['cid'], ]); DB::commit(); } catch (Throwable $th) { DB::rollBack(); report($th); throw new BizException('绑定失败,请稍后再试'); } return response()->noContent(); } }