PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] How to update only one input in Laravel form?

 March 01, 2022     laravel, laravel-5, php     No comments   

Issue

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'
  ]);

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?


Solution

Try this code:

$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.



Answered By - RoshJ
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing