mtvs / eloquent-hashids

On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
292 stars 21 forks source link

$item->hashidToId($hashid); Question #6

Closed garbinmarcelo closed 5 years ago

garbinmarcelo commented 5 years ago

Hello, how are you? First I would like to thank you for the library. I was already using vinkla/hashids, but yours already helped to dry the code.

But a question arose as to how the $item->hashidToId($hashid);method works, could you explain how to use it? Is it possible to use it without an instance of the model (as a helper)?

Thank you

P.S.: I was confused because of this $item-> I don't know where it is coming from or being instantiated. But I think it's from the model, right?

mtvs commented 5 years ago

Hi Marcelo, I'm happy that it's been useful.

public function hashidToId($hashid)
{
    return @Hashids::connection($this->getHashidsConnection())
        ->decode($hashid)[0];
}

The responsibility of this method is to decode a hashid belonging to the models of the class by using the connection (options) used for encoding the hashids of the model.

It's possible to use it, and it's used, on an empty model (new Model()) - again by using the connection options of the model.

garbinmarcelo commented 5 years ago

Perfect, that's what I would like to know. Thanks for your return. greetings