Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

LogicException Route pattern "/profile/{slug}/{{slug}}" cannot reference variable name "slug" more than once.

I've been getting this error

LogicException Route pattern "/profile/{slug}/{{slug}}" cannot reference variable name "slug" more than once.

Routes

// User Profile
Route::resource('profile/{slug}', 'ProfileController');

// Vendor Profile
Route::resource('vendor-profile/{slug}', 'VendorController');

// Agent Profile
Route::resource('agent-profile/{slug}', 'AgentController');

Controller

/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(User $slug)
    {
        return view('user.profile-index', compact('slug'));
    }
0 likes
18 replies
Sinnbeck's avatar

I assume you set route model binding to slug also?

Route::resource('profiles', 'ProfileController');
tomasosho's avatar

No i did not,

All i did was user slug as my keyname in my user model

public function getRouteKeyName()
    {
        return 'slug';
    }

When i use this Route::resource('profile', 'ProfileController');, my user properties does not appear, and i'm not trying to use Auth::user()->property

Sinnbeck's avatar

What do you get if you do

public function index(User $profile)
    {
        dd($profile);
    }
tomasosho's avatar

Empty

App\User {#1339 ▼
  #fillable: array:18 [▶]
  #hidden: array:2 [▶]
  #casts: array:1 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
}
Snapey's avatar

Where do you get this error message (file and line)

1 like
tomasosho's avatar
Symfony\Component\Routing\RouteCompiler::compilePattern
vendor/symfony/routing/RouteCompiler.php:149

NOTE: I only get the error message when i include /{slug} in my web route

Sinnbeck's avatar

Uhm did you change the index method like I showed ? To name the variable $profile instead of $slug

And change the url to profiles instead of profile

Perhaps show how it is currently?

1 like
tomasosho's avatar

Do i get to replace the {slug} in the route with {profile} too?

If i don't use the {profile} it's empty and if i use the {profile}, i get that error.

tomasosho's avatar
// User Profile
Route::resource('profiles/{profile}', 'ProfileController');

// Vendor Profile
Route::resource('vendor-profile/{profile}', 'VendorController');

// Agent Profile
Route::resource('agent-profile/{profile}', 'AgentController');

I am running vendor-profile/{profile}, it's giving me the error. if i remove

// User Profile
Route::resource('profiles/{profile}', 'ProfileController');

it runs smoothly.

Same thing applies to

// Vendor Profile
Route::resource('vendor-profile/{profile}', 'VendorController');

and

I am running ```vendor-profile/{profile}```, it's giving me the error.
if i remove 
tomasosho's avatar

Same thing

LogicException
Route pattern "/profiless/{sl}/{{sl}}" cannot reference variable name "sl" more than once.
http://127.0.0.1:8000/vendor-profile/vendor123

Controller Profile

public function show(User $sl)
    {
        return view('user.profile-index', compact('sl'));
    }

Controller Vendor

public function show(User $sl)
    {
        return view('user.vendor-profile', compact('sl'));
    }

Route

// User Profile
Route::resource('profiless/{sl}', 'ProfileController');

// Vendor Profile
Route::resource('vendor-profile/{slug}', 'VendorController');

// Agent Profile
Route::resource('agent-profile/{slug}', 'AgentController');
Sinnbeck's avatar

Just try with one at a time. Comment out the others

Route::resource('profiles', 'ProfileController');

//and controller 
public function show(User $profile)
    {
        return view('user.profile-index', compact('profile'));
    }
tomasosho's avatar
App\User {#1323 ▼
  #fillable: array:18 [▶]
  #hidden: array:2 [▶]
  #casts: array:1 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
}

Still the same 😭

tomasosho's avatar

I am getting null

    public function show($profile)
    {
        dd(User::find($profile));
        return view('user.vendor-profile', compact('profile'));
    }

link

http://127.0.0.1:8000/vendor-profile/vendor123

Route

Route::resource('vendor-profile', 'VendorController');
MichalOravec's avatar
Level 75

In that case you have to use where

User::where('slug', $profile)->firstOrFail();

getRouteKeyName works only for route model binding

1 like
tomasosho's avatar

i want to bind the route, how do i do this?

Sinnbeck's avatar

I honestly havent got a clue why my solution didnt work for you.. Just tested locally with using email instead of slug and it works perfectly..

web.php

Route::resource('profiles', ProfileController::class);

ProfileController::show

 public function show(User $profile)
    {
        dd($profile);
    }

User model (same as yours but with email instead of slug)

public function getRouteKeyName()
    {
        return 'email';
    }

And url http://localhost:8099/profiles/[email protected]

https://i.imgur.com/UkKSIR7.png

You dont need to get the user manually if you dont typehint the model (or you mess something up)

Btw. I used email instead of slug as I didnt want to add a new column, but it works exactly the same

If you still cant get it working, I might put it on github so you can try it out yourself :)

Please or to participate in this conversation.