Find User in Laravel by Username

前端 未结 3 855
说谎
说谎 2021-01-17 09:07

So typically if you have access to the id of a user in laravel you can run User::find($id), however say you don\'t have access to the user\'s id and only their username. Is

相关标签:
3条回答
  • 2021-01-17 09:24
    $user_id = DB::table('users')->where('username', $user_input)->first();
    

    without "->id"

    check here: http://laravel.com/docs/5.0/queries

    0 讨论(0)
  • 2021-01-17 09:32

    Yes, even better using the model. just like this

    User::where('username','John') -> first();
    // or use like 
    User::where('username','like','%John%') -> first();
    User::where('username','like','%John') -> first();
    User::where('username','like','Jo%') -> first();
    
    0 讨论(0)
  • 2021-01-17 09:43

    It depends. If a user is logged in you can have any information you want by:

    $field = Auth::user()->field;
    

    But if they are not logged in and you just want their user_id you can use:

    $user_id = User::select('id')->where('username', $username)->first();
    
    0 讨论(0)
提交回复
热议问题