6

I am trying to select tasks by user('id'), but I can't get it in a Controller, where I selecting data from DB. I have tried many thing and some of them from stackoverflow, but it isn't working.

I tried:

1. $userId = Auth::check() ? Auth::id() : true;
2. Auth::user()->id;
3. public function getUserId(){
   
   on Model} - and then get this value on Controllers

and some other things

I have the simplest code:

  1. I installed registration: npm artisan ui --auth something like that
  2. I installed vuejs (on Laravel)
  3. I created api on Laravel, and some logic on vue

I didn't touch "app.blade.php" it's the same as it was.

I can get data, user: name, id and all what I want in file "app.blade.php" but I need those data in folder->file: App\Http\Controllers{{SomeController}}, but I don't know how.

Was someone in this situation? How can I get user id in Controllers?

Thanks guys for earlier.

K.Igor
  • 143
  • 1
  • 2
  • 10

3 Answers3

16

If you need user id, just use one of this :

auth()->id();

using Auth facade's

\Auth::id();

or, using Request instance

$request->user()->id

Follow this simple controller code, i showed 3 different way here :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function getUserId(Request $request)
    {
        $user = Auth::user(); // Retrieve the currently authenticated user...
        $id = Auth::id(); // Retrieve the currently authenticated user's ID...

        
        $user = $request->user(); // returns an instance of the authenticated user...
        $id = $request->user()->id; // Retrieve the currently authenticated user's ID...

        
        $user = auth()->user(); // Retrieve the currently authenticated user...
        $id = auth()->id();  // Retrieve the currently authenticated user's ID...
    }
}
muhive
  • 1,879
  • 14
  • 20
  • I have 2 guards 1) user 2) admin. If both or any of them are logged in then how can I get the logged in front user or admin user information? `auth()->guard('admin')->email()` not working. Please suggest. Thanks. – Kamlesh Dec 09 '21 at 13:05
2
Auth::user()->id;

This should work if you have Auth middleware on that controller method where you try to get it, please check do you added that middleware. For checking you can use php arisan route:list command.

Vahe Galstyan
  • 1,681
  • 1
  • 12
  • 25
  • Yes, I have auth in column "Middleware" when I checked like you said through command php artisan route:list. – K.Igor Nov 11 '20 at 13:53
  • When I trying Auth::user()->id; Laravel saying that "id" isn't available in Auth. – K.Igor Nov 11 '20 at 13:55
  • @K.Igor What is of output of \Auth::user() ? – Vahe Galstyan Nov 11 '20 at 13:57
  • If I output in "app.blade.php" then it returns Array with user data. If I try to get data from Auth::user() from controller to vuejs and on console.log() then I get object with back request, like: headers, status, statusText and so on... – K.Igor Nov 11 '20 at 14:17
  • I've tried output "Auth::user()" throught Route and Controller on a page and it's showed empty. – K.Igor Nov 11 '20 at 16:51
1

Is someone still searching an answer on this question. I have some explanation how can you do this.

Laravel has a Router which routes authorization process through that Controller which you want, so you should redirect that process on your Router and in Controller create constructor which allows you to take user id.

How can you do that?:

1. First of all you should find Controller and Route which responsible for authorization and registration users. In my case it was: a)App\Http\Controllers\HomeController b)routes\web.php

2. Second, you should redirect your authorization Router to the Controller where you trying to get Auth::id(); In my case it was: App\Http\Controllers\TasksController

so, in routes\web.php I did this:

    //was
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    //was
    Auth::routes();
    //was
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    //added
    Auth::routes();
    //added
    Route::get('/home', [App\Http\Controllers\TasksController::class, 'index'])->name('home');

perhaps you should have index function on that controller

3. Third you should add constructor in your controller where you want to get user id, this constructor I took from HomeController, it already was there. In my case it was:

    public function __construct()
    {
        $this->middleware('auth');
    }

code with function on my TasksController:

    public function __construct()
    {
        $this->middleware('auth');
    }
       
    public function index()
    {              
        $userId = Auth::check() ? Auth::id() : true;       
    }

And now I can take user id. PS: constructor I added on the top in class TasksController in Controller

K.Igor
  • 143
  • 1
  • 2
  • 10