我試圖創建laravel查詢,如果在電子郵件等於$電子郵件記錄存在一個用戶名,這將檢查。我的表被稱爲用戶。Laravel選擇其中存在查詢
我需要這個查詢,因爲當用戶登記,他們只與他們的電子郵件地址和密碼進行註冊,並在以後的時間添加用戶名,所以我需要檢查,如果他們增加了一個用戶名呢。
我已經在這裏檢查文檔:https://laravel.com/docs/5.4/queries和無法理解我需要使用,任何人都可以幫助其查詢?
我試圖創建laravel查詢,如果在電子郵件等於$電子郵件記錄存在一個用戶名,這將檢查。我的表被稱爲用戶。Laravel選擇其中存在查詢
我需要這個查詢,因爲當用戶登記,他們只與他們的電子郵件地址和密碼進行註冊,並在以後的時間添加用戶名,所以我需要檢查,如果他們增加了一個用戶名呢。
我已經在這裏檢查文檔:https://laravel.com/docs/5.4/queries和無法理解我需要使用,任何人都可以幫助其查詢?
你可以這樣做:如果你只需要檢查是否存在不讀取用戶數據
$user = User::where('email', $email)->first();
if (empty($user->username)) {
// username is empty
} else {
echo $user->username;
}
我想你想要的東西是一樣的東西$id = DB::table('user')->where('email', $email)->whereNotNull('username')->value('id');
。這將返回匹配該電子郵件並且沒有用戶名的用戶的ID,否則它將返回空值。那是你要求的嗎?如果不是,你能澄清你的意思嗎?
,更有效的方法是:
$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
}
完美的作品,謝謝! – JL9