0

Hey guys so I made a route:

Route::get('/dashboard/{user}', [DashboardController::class, 'show'])->name('dashboard.show'); My controller is

public function show($id)
    {
        return view('dashboard.profile')->with('name',User::where($id));
    }

How do pass it into the view? so I get only data from the current user / userid

shaedrich
  • 5,457
  • 3
  • 26
  • 42
m0n0l0g
  • 21
  • 5
  • If I understand correctly, you pass in a user ID to the view per route, but you are actually not interested in that user, but rather the user who is currently logged in? In that case this may help: https://stackoverflow.com/questions/45257981/how-to-get-logged-in-user-data-into-laravel-controller – Stefan May 19 '21 at 10:24

2 Answers2

2

You can simplify it to this by using route model binding:

public function show(User $user)
{
    return view('dashboard.profile', [ 'user' => $user ]);
}
shaedrich
  • 5,457
  • 3
  • 26
  • 42
0

You Can Do This:

public function show(User $user)
{
    return view('dashboard.profile', [ 'user' => $user ]);
}

Or :

    public function show($id)
    {
        $user = User::findOrFail($id);
        return view('dashboard.profile', [ 'user' => $user ]);
    }

Or :

 public function show($id)
 {
     $user = User::where("id",$id)->first();
     return view('dashboard.profile', [ 'user' => $user ]);
 }

If You Need Authenticated user use :

auth()->user