modi
modi

Reputation: 337

Route not defined when passing parameter to a route

I have a named route with a parameter which looks like this...

Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');

Now in one of my controller,

I have a function that calls this route like this

public function _myFunction($some_data) {
    return redirect()->route('profile', [$some_data->user_id]);
}

and in my ProfileController's profile() function, I have this.

public function profile() {

    return view('modules.profile.profile');

}

I've followed the documentation and some guides I found in SO, but I got the same error,

"Route [profile] not defined."

can somebody enlighten me on where I went wrong?

Here's what my routes/web.php looks like...

Route::middleware(['auth:web'])->group(function ($router) {

    Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');

});

Upvotes: 0

Views: 1008

Answers (2)

modi
modi

Reputation: 337

I solved the issue, and its really my bad for not providing a more specific case information and made you guys confused.

I was using socialite and called _myFunction() inside the third party's callback..

After all, the problem was the socialite's google callback, instead of placing the return redirect()->route('profile', [$user->id]) inside _myFunction(), what I did was transfer it to the callback function.

So it looked like this now...

private $to_pass_user_id;
public function handleGoogleCallback()
{
    try {

        $user = Socialite::driver('google')->user();

        $this->_myFunction($user);

        return redirect()->route('profile', [$this->to_pass_user_id]);

    } catch (Exception $e) {

        dd($e->getMessage());

    }
}

public function _myFunction($some_data) {
 
    ... my logic here
    $this->to_pass_user_id = $some_value_from_the_logic
}

Upvotes: 0

ByWaleed
ByWaleed

Reputation: 402

When calling the route, you should pass the name of the attribute along with the value (as key vaue pairs). In this case, your route is expecting user_id so your route generation should look like this:

return redirect()->route('profile', ['user_id' => $some_data->user_id]);

Read more on Generating URLs To Named Routes in Laravel.

Upvotes: 0

Related Questions