我在routes / api.php中使用Laravel 5.5 API路由来注册这样的新用户......
Route :: post(' users /',function(Request $ request){
$user = new User;
if (Input::get('name')) {
$user->name = $request->input('name');
}
if (Input::get('email')) {
$user->name = $request->input('email');
}
if (Input::get('password')) {
$user->name = $request->input('password');
}
$user->save();
)};
但是如果我在数据库中已有一个匹配的电子邮件地址,那么我会收到一个SQL错误,因为该字段设置为唯一。
在尝试将电子邮件发送到数据库之前,如何检查电子邮件是否重复?
答案 0 :(得分:3)
有几种方法可以做到这一点。一,使用FormRequests并定义规则:
public function rules()
{
return [
'email' => 'required|unique:users'
];
}
两,使用控制器上的ValidatesRequests特性并在方法中调用validate
:
$this->validate($request, [...rules...])
如果验证失败,它会将用户重新定向并返回错误,或者如果ajax,则使用错误包发送json响应。
https://laravel.com/docs/5.5/validation#creating-form-requests
https://laravel.com/docs/5.5/validation#quick-writing-the-validation-logic
答案 1 :(得分:1)
请尝试以下。
# Hyperparameter definition
############################################################
epochs = 100000 # Global steps
t_max = 1000 # Thread steps
max_grad_norm = 0.5
alpha = 0.99
gamma = 0.99
#############################################################
ckpt_path = 'ckpt/checkpoints'
log_path = 'logs'
def test(ac, load_path, sess):
ac.load(load_path)
env = gym.make('Breakout-v0')
while True:
obs = env.reset()
state_c, state_h = ac.init_lstm_c, ac.init_lstm_h
done = False
while not done:
action, _, state_c, state_h = ac.step(sess, obs, state_c, state_h)
obs, _, done, _ = env.step(action)
def train(n_epochs, t_max, gamma, ac, sess):
env = gym.make("Breakout-v0")
save_dir = os.path.join(log_path)
writer = tf.summary.FileWriter(save_dir)
for ep in range(n_epochs):
ep_obs, ep_disc_rew, m_rew, ep_act, ep_vals, state_c, state_h = process_episode(sess, ac, env, t_max, gamma)
log = ac.learn(sess, ep_obs, state_c, state_h, ep_disc_rew, m_rew, ep_act, ep_vals)
step = tf.train.get_global_step().eval(session=sess)
writer.add_summary(log, global_step=step)
def process_episode(sess, ac, env, t_max, gamma):
ep_observations, ep_rewards, ep_actions, ep_values = [], [], [], []
done = False
t = 0
observation = env.reset()
state_c, state_h = ac.c_init, ac.h_init
while t < t_max and not done:
action, value, state_c, state_h = ac.step(sess, observation, state_c, state_h)
ep_observations.append(observation)
ep_values.append(value)
ep_actions.append(action)
observation, reward, done, _ = env.step(action)
ep_rewards.append(reward)
ep_disc_rewards = discount_rewards(ep_rewards, gamma)
t_rew = np.sum(ep_rewards)
return ep_observations, ep_disc_rewards, t_rew, ep_actions, ep_values, state_c, state_h
if __name__ == '__main__':
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
server = tf.train.Server.create_local_server()
gs = tf.train.create_global_step(tf.get_default_graph())
env = gym.make("Breakout-v0")
ac = ActorCritic(env.observation_space, env.action_space)
with tf.train.MonitoredTrainingSession(
master=server.target, checkpoint_dir=ckpt_path, save_summaries_steps=None, config=config) as sess:
train(epochs, t_max, gamma, ac, sess)
sess.stop()