Thursday, April 13, 2017

Decoding and using a uri parameter in laravel

I have the following link http://localhost:8000/mercedes-actorss/0AdrMB5Q3aObVw2r4BWJjpNo91xq6KeX

This part is the slug mercedes-actorss and this part is the id which has been encoded with Hashid 0AdrMB5Q3aObVw2r4BWJjpNo91xq6KeX

That can be decoded like this

$s = '0AdrMB5Q3aObVw2r4BWJjpNo91xq6KeX';
    $h =  Hashids::encode(2);
    $d = Hashids::decode($s);
    echo $h;
    echo '<br/>';
    echo $d[0];

However, i want to use the id in my controller like this,

 public function show_post($slug = null, $param = null){
            $para = Hashids::decode($param);
            $p = (int)$para[0];

            $name = 'Jane Doe';
            $city = 'Pittsburg';

            $tasks = ['one',2,'three'];

            $posts = Text::where('model','=',$slug)->where('id','=',$p)->first();

            return view('show-post',compact('posts'));
        }

The code does not fetch the records as i would have wanted. I am i decoding correctly in my second controller function?.



via Geoffrey Isle

Advertisement