php - Use Hashids library to hash ids on Laravel eloquent collection -


i'm grabbing set of tasks database eloquent collection, i'm sending collection view foreach. no problems here. except, need reference task id in view (url action, etc.). don't want in source, i'm using library hash id. doing in view seems wrong.

is there way hash id in model or controller?

here's how i'm calling collection in controller:

$tasks = auth::user()->tasks()->orderby('created_at', 'desc')->get(); 

this how i'm hashing id in view:

<a href="{{ route('tasks.markascompleted', hashids::encode($task->id)) }}"> 

any help?

you using accessor method. first, append new attribute @ top of task model:

protected $appends = ['hashid']; 

then, in same model, create accessor populates attribute:

public function gethashidattribute() {     return hashids::encode($this->attributes['id']); } 

once have those, call appended attribute in view:

<a href="{{ route('tasks.markascompleted', $task->hashid) }}"> 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -