User8392
User8392

Reputation: 113

laravel query to compare two string from DB

I used laravel's query builder.

$email=DB::table('users')->where('id',1)->select('e_mail')->get();
dd($email);

and, this is result.

Collection {#231 ▼
  #items: array:1 [▼
    0 => {#229 ▼
      +"e_mail": "[email protected]"
    }
  ]
}

but test code's result was False.

$email=DB::table('users')->where('id',1)->select('e_mail')->get();
"[email protected]"!=$email[0]

I found other questions, but didn't find solution.
please your advise. thanks.

Upvotes: 1

Views: 3433

Answers (3)

Mohit Prajapati
Mohit Prajapati

Reputation: 66

Please use it like this:

$email=DB::table('users')->where('id',1)->select('e_mail')->get();

"[email protected]"!=$email[0]->e_mail

Upvotes: 1

ZeroOne
ZeroOne

Reputation: 9117

you are finding using id.. so i assume it is unique.. if using get() it will return as array.

use Eloquent..

$user = Users::select('e_mail')->find(1);
// $user = Users::select('e_mail')->where('id', 1)->first(); //this also can

if("[email protected]" != $user->email) 
{
    //
}

or Query..use first();

$user=DB::table('users')->where('id',1)->select('e_mail')->first(); 

if("[email protected]" != $user->e_mail) 
{
    //
}

Upvotes: 0

Milan Chheda
Milan Chheda

Reputation: 8249

From your output, it seems instead of $email[0], you need to use $email[0]['e_mail'], if it's an array, or you need to use $email[0]->e_mail, if it's an object.

Upvotes: 1

Related Questions