首页 文章

Laravel检查是否存在记录

提问于
浏览
168

我是Laravel的新手 . 请原谅新手问题,但我如何找到记录是否存在?

<?php

$user = User::where('email', '=', Input::get('email'));

我可以在这做什么来查看 $user 是否有记录?

14 回答

  • 3

    最短的工作选择:

    // if you need to do something with the user 
    if ($user = User::whereEmail(Input::get('email'))->first()) {
    
        // ...
    
    }
    
    // otherwise
    $userExists = User::whereEmail(Input::get('email'))->exists();
    
  • 368
    $user = User::where('email', request('email')->first();
    return (count($user) > 0 ? 'Email Exist' : 'Email Not Exist');
    
  • 0

    if 语句中检查 null 会阻止Laravel在查询结束后立即返回404 .

    if ( User::find( $userId ) === null ) {
    
        return "user does not exist";
    }
    else {
        $user = User::find( $userId );
    
        return $user;
    }
    

    如果找到用户,它似乎运行双重查询,但我似乎找不到任何其他可靠的解决方案 .

  • 16

    这取决于您是否希望之后与用户合作或仅检查是否存在 .

    如果要使用用户对象(如果存在):

    $user = User::where('email', '=', Input::get('email'))->first();
    if ($user === null) {
       // user doesn't exist
    }
    

    如果你只想检查

    if (User::where('email', '=', Input::get('email'))->count() > 0) {
       // user found
    }
    

    甚至更好

    if (User::where('email', '=', Input::get('email'))->exists()) {
       // user found
    }
    
  • -1

    In your Controller

    $this->validate($request, [
            'email' => 'required|unique:user|email',
        ]);
    

    In your View - Display Already Exist Message

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    
  • 0

    最好的解决方案之一是使用 firstOrNewfirstOrCreate 方法 .

  • 0
    $user = User::where('email', '=', Input::get('email'))->first();
    if ($user === null) {
       // user doesn't exist
    }
    

    可写成

    if (User::where('email', '=', Input::get('email'))->first() === null) {
       // user doesn't exist
    }
    

    如果您在原始语句中使用$ user for,那么这将返回true或false而不分配临时变量 .

  • 6
    if (User::where('email', Input::get('email'))->exists()) {
        // exists
    }
    
  • 0
    if($user->isEmpty()){
        // has no records
    }
    

    Eloquent使用集合 . 请参阅以下链接:https://laravel.com/docs/5.4/eloquent-collections

  • 0

    Laravel 5.6.26v

    通过主键(电子邮件或身份证)查找现有记录

    $user = DB::table('users')->where('email',$email)->first();
    

    然后

    if(!$user){
                 //user is not found 
          }
          if($user){
                 // user found 
          }
    

    包括“使用DB”和表名用户使用上述查询(如用户对用户)变为复数

  • 0

    你可以使用laravel验证 .

    但是这段代码也很好:

    $user = User::where('email',  $request->input('email'))->count();
    
    if($user > 0)
    {
       echo "There is data";
    }
    else
       echo "No data";
    
  • 1

    知道是否有任何记录很简单

    $user = User::where('email', '=', Input::get('email'))->get();
    if(count($user) > 0)
    {
    echo "There is data";
    }
    else
    echo "No data";
    
  • -6

    this is simple code to check email is exist or not in database

    $data = $request->all();
        $user = DB::table('User')->pluck('email')->toArray();
        if(in_array($user,$data['email']))
        {
        echo 'existed email';
        }
    
  • 0

    这将检查表中是否存在特定的电子邮件地址:

    if (isset(User::where('email', Input::get('email'))->value('email')))
    {
        // Input::get('email') exist in the table 
    }
    

相关问题