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,510 points
13,762 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,880 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,880 points
8,073 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,880 points
26,136 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,880 points
500 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,880 points
25,451 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,880 points
21,756 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,880 points
2,647 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,880 points
2,506 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,880 points
41,379 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,880 points
2,287 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,880 points
4,269 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP