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

slickness's avatar

I tried all your suggestions, but nothing works. I also tried to change the RouteKey But it does not work either.

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

This is my route:

Route::resource('tags/', 'TagController');
Snapey's avatar

ok. assuming your code is still like this;

 public function show($name)
    {
        $tag = Tag::where('name', $name)->get();

        dd($tag);

        return view('tag', compact('tag'));
    }

getRouteKeyName will have no effect if you do not typehint the Tag model in the method.

ie

public function show(Tag $tag)
{
    return view('tag', compact('tag'));
}

or keep your code and dd($name)

Are you getting the tag's name or its id ?

Do you have a tag in your database with that name or id

What is the schema of your tags?

slickness's avatar

When I use this code:

public function show($name)
    {
        $tag = Tag::where('name', $name)->get();

        dd($name);

        return view('tag', compact('tag'));
    }

I get with dd($name) only the name after tags... for exampel at the url tags/test it shows me only "test".

When I use this code:

public function show(Tag $tag)
{
    return view('tag', compact('tag'));
}

with this code in the Tag model:

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

    public function setNameAttribute ($name){
        $this->attributes['name'] = strtolower($name);
    }

I get nothing. It shows me: "Sorry, the page you are looking for could not be found."

Then I deleted the # at one tag in the database and then I could call the page. This means that the # causes the problem. But how can I solve this problem?

The tag database is only with the name and the id. For exampel:

id name 1 #test 2 #show 3 #return

Snapey's avatar

Well I asked you if you had a name the same in the database. ie, 'test' you did not say that your tags are #test

Of course it won't match

You cannot put # in the URL as it is a reserved character

Remove the # from the database or fix it in the controller (but its just code for no good reason)

public function show($name)
    {
        $tag = Tag::where('name', '#' . $name)->get();

        return view('tag', compact('tag'));
    }
slickness's avatar

It gives me the following error message: Property [name] does not exist on this collection instance.

lostdreamer_nl's avatar
public function show($name)
    {
        // $tag = Tag::where('name', '#' . $name)->get();
        $tag = Tag::where('name', '#' . $name)->first();

        return view('tag', compact('tag'));
    }

1 like
Snapey's avatar

You have a name field in your tags table?

slickness's avatar

it works with this code:

public function show($name)
    {
        // $tag = Tag::where('name', '#' . $name)->get();
        $tag = Tag::where('name', '#' . $name)->first();

        return view('tag', compact('tag'));
    }
Previous

Please or to participate in this conversation.