0

So I have a route profile/{user_id}

How do I redirect user to that URL when they click on link?

Here's my controller:

{
    function checkid($user_id) {
      if (Auth::check())
      {
        $user_id = Auth::id();
        return view('profile', [
        'id' => $user_id
        ]);
      }
    }
}
4
  • 1
    What list? Where's your controller? Have you proof-read your question before posting it? Commented Jun 5, 2017 at 15:12
  • misstyped sorry, it was supposed to say "link" Commented Jun 5, 2017 at 15:13
  • What link? There isn't any link in this question. There's no controller either. Commented Jun 5, 2017 at 15:14
  • @Przemek do you make it a habit of asking questions and never marking the answers? return redirect("profile/{$user_id}"); Commented Jun 5, 2017 at 15:19

2 Answers 2

2

Bit confused with the question but Laravel uses ID as default for dependency injection and changing it is easy: just change the routeKey in the model BUT in your instance, you're using the signed in user. So forgot the id!

<?php

    namespace App\Http\Controllers;

    class RandomController extends Controller {
        public function index()
        {
            return view('profile');//use auth facade in blade.
        }
    }

In your routes use a middleware to prevent none authenticated users from reaching this method

<?php

Route::group('/loggedin', [
    'middleware' => 'auth',
], function() {

    Route::get('/profile', 'RandomController@index')->name('profile.index');

});

Now to redirect in your blade file use the route function, don't forget to clear your cache if you've cached your routes!

<h1>Hi! {{Auth::user()->name}}</h1>
<a href="{{route('profile.index')}}">View profile</a>

Because I used the name method on the Route I can pass that route name into the route function. Using php artisan route:list will list all your route parameters which is cool because it will also tell you the names and middlewares etc.

if I had a route which required a parameter; the route function accepts an array of params as the second parameter. route('profile.index', ['I_am_a_fake_param' => $user->id,]).

Let me know if you need help with anything else.

Sign up to request clarification or add additional context in comments.

Comments

0

You can redirect with the redirect() helper method like this:

return redirect()->url('/profile/' . $user_id);

But I'm not really following your usecase? Why do you want to redirect? Do you always want the user to go to their own profile? Because right now you are using the id from the authenticated user, so the user_id parameter is pretty much useless.

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.