通过用户名在Laravel中查找用户 [英] Find User in Laravel by Username
本文介绍了通过用户名在Laravel中查找用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,通常,如果您有权访问laravel中的用户ID,则可以运行User :: find($ id),但是说您无权访问该用户的ID,而只能访问其用户名.是否有比使用DB :: query定位用户更好的方法?
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 there a better way than using DB::query to locate the user?
这是我当前的解决方案,想知道是否有人可能知道更好的方法.
This is my current solution and was wondering if someone possibly knew a better way.
$user_id = DB::table('users')->where('username', $user_input)->first()->id;
推荐答案
是的,使用模型甚至更好.就是这样
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();
这篇关于通过用户名在Laravel中查找用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文