mtvs / eloquent-hashids

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

Feature request: validation rule #15

Closed andrewminion-luminfire closed 4 years ago

andrewminion-luminfire commented 4 years ago

I’m using eloquent hashids on a number of related objects, and often I am passing a related_model_id as part of my request.

Currently I’m only validating it as string|required but it would be great to be able to validate that the model actually exists, like the exists:table,column validation rule.

Is that something you’d consider adding?

mtvs commented 4 years ago

I think it would probably be a better idea to just pass the actual id of the model. A hashid is more useful when is displayed to users.

andrewminion-luminfire commented 4 years ago

My app has a JS frontend and the hashids are visible and used by users and in the API requests it makes.

mtvs commented 4 years ago

I would use hasids in views and original ids in api calls.

limsocheat commented 4 years ago

@andrewminion-luminfire

you can try to

$item = Outlet::findByHashidOrFail($hashid);
$id = $item->hashidToId($hashid);

$request->validate([
'name' => 'required|unique:outlets,name,' . $id,
]);