where('work_station_type', $wsType) ->first(); if($res) { if($wsName){ $res->name = $wsName; $res->token = md5($wsId.'.'.$wsType.'.'.$wsName); $res->save(); } }else { $model = new self(); $model->name = $wsName; $model->work_station_type = $wsType; $model->work_station_id = $wsId; $model->token = md5($wsId.'.'.$wsType.'.'.$wsName); if($model->save()){ $res= $model; } } return $res; } }