0

I can get the Auth ID when i put it in any controller with

Auth::user()->id

But when i put it in AppServiceProvider.php , it returns `Trying to get property 'id' of non-object

i don't understand why ?

Eddit : I tried this but still not working

public function boot()
{
    view()->composer('*', function ($view) 
{
if (Auth::check())
{
    $id=Auth::user()->id;
    $idd=Person::where('user_id','=',$id)->get('photo');

    $view->with('idd', $idd );
    $view->with('id', $id );
}
       });
}

Error : Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in

yassine j
  • 495
  • 1
  • 11
  • 27
  • 4
    possible duplicate: https://stackoverflow.com/questions/37372357/laravel-how-to-get-current-user-in-appserviceprovider – nakov Nov 10 '18 at 17:38
  • How are you authenticating the user? – Kenny Horna Nov 10 '18 at 17:46
  • 1
    Possible duplicate of [Laravel - How to get current user in AppServiceProvider](https://stackoverflow.com/questions/37372357/laravel-how-to-get-current-user-in-appserviceprovider) – Marwelln Nov 10 '18 at 18:53
  • Hello, thank you for ur interest. I saw the duplicate question and i eddited post for you. Still not working. Can you help please ? – yassine j Nov 10 '18 at 21:16

3 Answers3

0

To get the currently authenticated user's ID, use Auth::id();

Another case may be that there is not a current user, in which case Auth::user() is returning NULL. Wrap the code in a

if (Auth::check())
{
    // Do stuff
}

to make sure there is a user logged in.

dc115
  • 127
  • 1
  • 9
0
view()->composer('*', function($view)

 {$view->with('user',auth()->user());

});
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 15 '22 at 00:37
-2

it's work for me

<?php

namespace Fitness\Providers;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Request $request)
    {
    view()->share('user', $request->user());
    }
}
iElden
  • 1,272
  • 1
  • 13
  • 26
DIO
  • 1