using slug in laravel

Published 7 months ago by DivPro

i want to use str_slug in

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

So The url changes from http://crazy-anime.com/animes/One%20Piece to http://crazy-anime.com/animes/one-piece

JackJones

What is stopping you?

rumm.an

Define a accessor for slug property in your Model, and return slug from getRouteKeyName method. Like so:

public function getSlugAttribute()
{
    return str_slug($this->anime_name);
}

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

@rumm.an When i'm using this Code

    public function getRouteKeyName()
{
    return str_slug($this->anime_name);
}

nothing happen to url and i get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'anime-name' in 'where clause' (SQL: select * from animes where anime-name = Detective Conan limit 1)

rumm.an

@DivPro I can clearly see you are using hyphen anime-name in your actual code. But, here you have posted an underscore anime_name.

DivPro

@rumm.an i tried your code and i'm getting same error

public function getRouteKeyName()
{
    return str_slug($this->anime_name);
}
rumm.an

Do you have an anime_name column in your table? You should post your migration for the model.

DivPro

@rumm.an yes i only get error when trying str_slug()

rumm.an

The above error is probably from a where clause where you have used anime-name. Maybe a typo.

rumm.an

Show your controller method and migration.

DivPro

@rumm.an Can We use Team Viewr

rumm.an

I think its okay if you post your relevant code here. Maybe other people can also figure out the issue.

DivPro

@rumm.an OK what code do you want me to past ?

rumm.an

Your controller method where you are facing the issue and your migration for the relevant model.

DivPro

@rumm.an for migration, i'm using voyager and my model

    public function getRouteKeyName()
{
    return 'anime_name';
}
rumm.an

can you post the screen shot of the error screen ?

Please sign in or create an account to participate in this conversation.