Adding more info to Laravel's auth user
Date : March 29 2020, 07:55 AM
|
How to share Auth::user among all views in Laravel 5?
Date : March 29 2020, 07:55 AM
I hope this helps you . Better off using View Composers for this one. In your ComposerServiceProvider in boot(): view()->composer('*', 'App\Http\ViewComposers\GlobalComposer');
public function compose( View $view )
{
$view->with('authUser', Auth::user());
}
|
How to stop Auth user from visiting own page with non auth user privileges in laravel?
Tag : php , By : orlandoferrer
Date : March 29 2020, 07:55 AM
may help you . You get user instance by Auth::user() not only the user ID. You are comparing instance with the numeric value. It will not work. You have to use Auth::id() or Auth::user()->id in order to get ID of the logged in user. The following code will work in your case. public function getProfile($id)
{
if(Auth::id() == $id)
{
redirect('dashboard');
}
else
{
$user = User::where('id', $id)->first();
$posts = Post::where("dash_id", "=", $user->id)->latest()->paginate(3);
$photos = Photo::paginate(6);
return view('profile.index',compact('user','posts', 'photos'));
}
}
|
Does Laravel Auth::user()->name and Auth::user()->email do two querys
Date : March 29 2020, 07:55 AM
To fix this issue It will do only one query. To remind my self of this I usually stick the user in the route and pass to the blade template: $user = Auth:user();
return view('yourview', ['user' => $user]);
<p>{{$user->id}}</p>
<p>{{$user->name}}</p>
<p>{{$user->email}}</p>
|
Laravel 5.8 @if(Auth::user() && Auth::user()->role_id == 2) returns an error
Date : March 29 2020, 07:55 AM
should help you out First check if there an authenticated user with @if(Auth::check()), and then nest the other conditions: @if(Auth::check())
@if(Auth::user()->role_id && Auth::user()->role_id == 2)
<a class="dropdown-item" href="/">Manage My Ad Images</a>
<a class="dropdown-item" href="/">My Ads</a>
<a class="dropdown-item" href="/">Post an Ad</a>
<a class="dropdown-item" href="/">Reload My Account</a>
@endif
@endif
|