Login with username or email Laravel

Login with username or email Laravel



We are going to see the controller on how to match data in a database using user input.





$nameOremail is the user input, We are passing it as an argument into controller function.



      
     export default {
        name:'Login',
        data:function(){
            return {
                form:{
                    nameEmail:'',
                    password:'',
                },
                errors:{
                    message:[]
                }
            }
        },
        methods:{
            processUser(){
            if(this.errors.message.length>0){
                this.errors.message.length = 0;
            }
            
                    if(this.form.nameEmail == ''){
                        this.errors.message.push("Add Name Or Email");
                    }
                    if(this.form.password == ''){
                        this.errors.message.push("You Need To Provide A Password");
                    }

                   axios.get('api/user/'+this.form.nameEmail)
                    .then(function (response) {
                        console.log(response);
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
                    .then(function () {
                        // always executed
                    });  


            }
        }
    }

We are using “axios” in the above code and sending values to “API/user/$nameOremail” format.





      $user = filter_var($nameOremail, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
         if($user == 'email'){
            $userData =  User::where('email',$nameOremail) -> first();
            return $userData;
         }
         
         if($user == 'name'){
            $userData =  User::where('name',$nameOremail) -> first();
            return $userData;
         }

Here in this code, we are checking if $nameOremail matches to “email” format if not then assign to “name”. and then check that value with our database if it is found or not. If found return “Userdata”.