0

I develop the register module , i want to check users registered in my web app with email , nationalCode or mobile , i have two tables , users and userInfo , i store email in users table and i store nationalCode and mobile in userInfo table , i want to write code to detect if email or nationalCode or mobile of the user exist in my two tables , i show warning text that user have registered in my site, please help me to do this job,

I use step form and i write ajax to call method to do this task, note that it may be possible teh user have three matches or just one of them is matched thanks for your helps :)

Here is the ajax code :

    $.ajax({
    url: url',
    type: 'POST',
    data: {
        _token: CSRF_TOKEN ,
        code:code,
        email:email,
        mobile:mobile,
    },
    dataType: 'JSON',
    success:function(data) {
        //return data
    }
});

and here is my method is controller

public function checkUser(Request $request)
{
    $email = $request->email;
    $mobile = $request->mobile;
    $code = $request->code;

    //here the query to detect user exist with three params
}
Amirreza Moradi
  • 171
  • 2
  • 15

2 Answers2

0

Let's say you have your relationships defined as follows:

class User extends Model
{
    public function info()
    {
        return $this->hasOne(UserInfo::class); 
    }
}

class UserInfo extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class); 
    }
}

... then you can check the existence of this user with something like this.

$user = User::where('email', $request->email)
    ->whereHas('info', function($query) use($request) {
        $query->where('mobile', $request->mobile)
            ->where('code', $request->code); 
    })
    ->exists();

// user will be false if there's no record matching those parameters

Alternatively, if you don't have your relationships defined, then you probably need to do something like this instead.

$user = User::where('email', $request->email)->exists();
$info = UserInfo::where([
    'mobile' => $request->mobile,
    'code'   => $request->code
])->exists(); 

if($user && $info) {
    // user exists 
}

I would still prefer to go with option one :)

Mozammil
  • 7,616
  • 12
  • 26
0

If you put unique identifier in you table, database will automatically detect it and return the error, but its not good practice to let database to handle that,

If you want to use Eloquent then the query would look like this

public function checkUser(Request $request)
{
    $email = $request->email;
    $mobile = $request->mobile;
    $code = $request->code;
    $user = User::query()->where('email', '=', $email)->orWhere('mobile','=',$mobile)
            ->orWhere('code', '=',$code)->get();
    if($user) {
     // User already exits
       return;
    }
}

But this validation for me is not good, Better is to use Laravel Requests https://laravel.com/docs/5.7/validation#form-request-validation

To generate custom request use this command (php artisan make:request RequestName)

public function rules()
{
    return [
        'title' => 'required|unique:users',
        'mobile' => 'required|unique:users',
        'code' => 'required|unique:users',
    ];
}

Using the request is simple

public function checkUser(YourCustomRequest $request)
{
    // Laravel will take care of all fields and check them if they exist in the database
}