2017-03-02 6 views
1

laravel에서 이메일이 $ email과 같은 레코드에 사용자 이름이 있는지 확인하는 쿼리를 만들려고합니다. 내 테이블을 사용자라고합니다.Laravel select where where queries

사용자가 등록 할 때 전자 메일 주소와 암호로 등록하고 나중에 사용자 이름을 추가하기 때문에이 사용자 이름을 추가했는지 확인해야하기 때문에이 쿼리가 필요합니다.

여기에서 문서를 확인했습니다 : https://laravel.com/docs/5.4/queries 어떤 검색어를 사용해야하는지 이해할 수 없습니까?

답변

1

당신은이 작업을 수행 할 수 있습니다

$user = User::where('email', $email)->first(); 

if (empty($user->username)) { 
    // username is empty 
} else { 
    echo $user->username; 
} 
+1

완벽하게 작동합니다. 감사합니다. – JL9

0

내가 원하는 것은 $id = DB::table('user')->where('email', $email)->whereNotNull('username')->value('id');과 같은 것입니다. 그러면 해당 이메일과 일치하고 사용자 이름이없는 사용자의 ID가 반환되거나 null이 반환됩니다. 그게 당신이 요구 한 것입니까? 그렇지 않다면 당신이 의미하는 바를 명확히 할 수 있습니까?

0

을 만 사용자 데이터를 읽지 않고 존재를 확인해야하는 경우, 더 효율적인 방법은 다음과 같습니다

$exists=User::where('email', $email)->whereNotNull('username')->exists(); 
if ($exists) { 
    //User exists. No other data about it. 
} 

그것을 확인하고 경우에만 자신의 데이터를 읽으려면 존재 :

$user = User::where('email', $email)->whereNotNull('username')->first(); 

if (!$user) { 
    //Not found with username, maybe exists without it 
} 

확인 후 데이터를 어쨌든 그것을 읽고하려면

$user = User::where('email', $email)->first(); 

if (!$user->username) { 
    //Does not exists even without username 
}else if (!$user->username) { 
    //Exists but without username 
}else{ 
    //Exists with username 
}