如何只更新Laravel形式的一个输入? [英] How to update only one input in Laravel form?

查看:41
本文介绍了如何只更新Laravel形式的一个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用laravel和mysql.我有带有 name email password conform password 的用户更新表单,并具有以下控制器验证,

working with laravel and mysql. I have user update form with name,email,password and conform password with following controller validation,

$this->validate($request, [
            'name' =>'required',
            'email' => 'required|email',
            'password' => 'sometimes|min:6|confirmed'
  ]);

在我的表单中,

通常,用户不会更新所有输入字段.例如,某些用户只需要更新电子邮件.但是通过以上验证,当我们仅更新名称或电子邮件验证显示消息时,密码必须至少包含6个字符.那么如何只更新名称或电子邮件而不进行密码更新呢?

in my form normaly users do not update this all input fields. as an example some user need update only email. but with above validation when we update only name or email validation display message The password must be at least 6 characters. then how can do only name or email update without password update?

推荐答案

尝试以下代码:

$this->validate($request, [
            'name' =>'required',
            'email' => 'required|email',
  ]);

  $name  = $request->name ;
  $email = $request->email;
  $q = User::where('id','=',auth()->user()->id);
  $q->update(['name'=>$name,'email'=>$email]);

  if(isset($request->password)){
       $this->validate($request,[
        'password' => 'required|string|min:6|confirmed',
       ]);
       $password = $request->password);
       $q->update(['password'=>$password );
  }

您也可以通过将更改密码以单独的形式放置来实现.

Also you can do it by putting changing password in seperate form.

这篇关于如何只更新Laravel形式的一个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆