2
$input = Input::all();


$user = User::where(function($q) {
          $q->where('email', $input['email'] )
            ->orWhere('email', $input['emails'][0]['value'] );
      })
      ->first();

   print_r($user);die;

Always says Undefined variable: input What is the best way to compare email with two values please guide

Amy
  • 163
  • 1
  • 11

2 Answers2

3

Add use($input) to pass the $input variable to the closure scope:

User::where(function ($q) use ($input) {

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.

http://php.net/manual/en/functions.anonymous.php

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • It says Undefined index: email – Amy Jul 28 '17 at 06:16
  • Array ( [kind] => plus#person [etag] => "s/s" [birthday] => 1996-08-18 [gender] => male [emails] => Array ( [0] => Array ( [value] => s@gmail.com [type] => account ) ) – Amy Jul 28 '17 at 06:17
  • 1
    @AmanJoshi that's completely different error. This means now you can access the `$input` variable inside the closure, but the variable doesn't have `email`. Do `$input[emails][0][0]['value']` instead of `$input['email']` – Alexey Mezenin Jul 28 '17 at 06:17
  • There are two condition where i get email and emails array need to set both – Amy Jul 28 '17 at 06:17
  • Yes @Alexey Mezenin what is the way it skip if email is undefined and will check emails array if exists. It can be either email variable or emails array i want check user's email from inputs value's – Amy Jul 28 '17 at 06:20
  • @Alexy see I'll get two value either email or emails array So here if I do check $input['emails'][0]['value'] will get exact value but there is no email key in array so it says invalid index email I want it will skip if email is undefined wheather you have emails array vice versa – Amy Jul 28 '17 at 06:23
0
User::where(function($q) use ($input) {
    $q->where('email', $input['emails'][0]['value']);
    if (array_key_exists('email', $input)) {
        $q->orWhere('email', $input['emails'][0]['value']);
    }
})

but make sure that you always have this value $input['emails'][0]['value']

skido
  • 452
  • 1
  • 4
  • 11