0

I am using the basic authentication system included in laravel package and have a user schema which contains the following:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

I am using Auth::user()->name to return the name of current user.

Is there a pre-defined function to return the name of user with a specific id?

If not, how can I do it?

3 Answers 3

1

If you are using Auth::user()->name to return the name of the user then you can use Auth::user()->id to return that user's id.

Please add more details to the question you are asking?

Auth::user() is basically a middleware that checks if the user is logged in or not. It will only return the value of the logged user.

Now if you want to get the data of a user using an id. You can user controller function..

first of all declare User class on top of controller

use User;

Then write the function

public function getUser($id)
{
    $user = User::where('id','=',$id)
    ->first();
    return $user;
}

to access it

go to web.php and write a route

Route::get('/user/{id}','YourController@getUser');

and now access it through

localhost:8000/user/1
Sign up to request clarification or add additional context in comments.

Comments

0

User is a model located here:

App\User;

You can reference this model and use Eloquent to do it by finding a specific user by their ID:

$user = App\User::find($id);

Alternative, you can use the fluid query builder if you prefer:

$user = Illuminate\Support\Facades\DB::table('users')->where('id', $id)->first();

Both of these methods will work correctly. It just depends on your needs.

Comments

0

I would sincerely recommend to go and read the docs.

$user = App\User::find($id);
// Check if null or use App\User::findOrFail($id)
$user->name();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.