How to Find User in Laravel by Username

0 votes

 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;
Dec 3, 2020 in Laravel by kartik
• 37,490 points
199 views

1 answer to this question.

0 votes

Hello,

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();
answered Dec 3, 2020 by Niroj
• 82,580 points

Related Questions In Laravel

0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,580 points
1,075 views
0 votes
1 answer

How to Use Order By for Multiple Columns in Laravel ?

Hii, Use order by like this: return User::orderBy('name', 'DESC') ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,580 points
1,059 views
0 votes
1 answer

How to “Refresh” the User object in Laravel?

Hello @kartik, You can update the cache object ...READ MORE

answered Dec 2, 2020 in Laravel by Niroj
• 82,580 points
44 views
0 votes
1 answer

How to redirect to Login if user not logged in Laravel?

Hello @kartik, You can use it in middleware. ...READ MORE

answered Dec 4, 2020 in Laravel by Niroj
• 82,580 points
1,007 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,580 points
5,638 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,580 points
652 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,580 points
497 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,580 points
6,217 views
0 votes
1 answer

How to “Refresh” the User object in Laravel?

Hello @kartik, You can update the cache object ...READ MORE

answered Sep 30, 2020 in Laravel by Niroj
• 82,580 points
224 views
0 votes
1 answer

How to invalidate all tokens for an user in laravel passport?

Hello @kartik, One of the methods it provides ...READ MORE

answered Oct 23, 2020 in Laravel by Niroj
• 82,580 points
358 views