vinkla / laravel-hashids Goto Github PK
View Code? Open in Web Editor NEWA Hashids bridge for Laravel
License: MIT License
A Hashids bridge for Laravel
License: MIT License
Problem 1
- Installation request for vinkla/hashids ^3.3 -> satisfiable by vinkla/hashids[3.3.0].
- Conclusion: remove laravel/framework v5.4.36
- Conclusion: don't install laravel/framework v5.4.36
- vinkla/hashids 3.3.0 requires illuminate/support 5.5.* -> satisfiable by illuminate/support[v5.5.0, v5.5.16, v5.5.17, v5.5.2].
- don't install illuminate/support v5.5.0|don't install laravel/framework v5.4.36
- don't install illuminate/support v5.5.16|don't install laravel/framework v5.4.36
- don't install illuminate/support v5.5.17|don't install laravel/framework v5.4.36
- don't install illuminate/support v5.5.2|don't install laravel/framework v5.4.36
- Installation request for laravel/framework (locked at v5.4.36, required as 5.4.*) -> satisfiable by laravel/framework[v5.4.36].
how can i fix that?
thanks
Esquece
Hey i just tried this package on Laravel 5.2 but its giving me back just characters and not numbers and characters.
Config File:
<?php
return [
'default' => 'main',
'connections' => [
'main' => [
'salt' => env('APP_KEY'),
'length' => 8,
'alphabet' => 'abcdefghijklmnopqrstuvwxyz',
],
'alternative' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
'alphabet' => 'your-alphabet-string',
],
],
];
Route File:
<?php
use App\User;
Route::get('/test', function () {
return Hashids::encode(12);
});
Output
reazwdkw
Conflicts with other installation packages
Hi, I'm using hashids
and would like to hash MongoDB generated _id
(ObjectID).
They seems like 59075c853bacf0e02965c690
.
Yes, it's not friendly to url.
However, I got fail when trying to encode such an ID like this,
Hashids::encode('59075c853bacf0e02965c690');
Got empty string ""
eventually.
Not only ObjectID
, but anything except number, will get empty string too.
How can I deal with a string or it just support numbers?
Thank you so much :)
I use the following code to decode a Hashid:
$value = Hashids::decode('doyouthinkthatsairyourebreathingnow')[0];
Now if the Hashid has no matches I Get an error "Undefined offset: 0". It gives an error status code of 500 whereas it should be 404 not found. Shouldn't an exception be thrown if Hashid decode fails? Or am I missing something?
There is an extra
: array
on the end of line 121 in HashidsServiceProvider.php which is breaking the package.
Hello.
I have i would say weird problem. I will say i am starter in Laravel, but i still find it weird.
I made some testing CRUD app in Laravel, and everything worked, now i installed Hashids, i set up config file and made encryption of my database id normally Hashids::encode($client->id) and that works. Then in Clients controller i did like this
public function edit($id) {
$client = Client::findOrFail(Hashids::decode($id));
return view('client.edit')
->with('client', $client);
}
when i do this, i went to client.blade.php and i made simple test
{{$client->name}}
and it throws error
Undefined property: Illuminate\Database\Eloquent\Collection::$name
but when i do
{{$client}}
it gives correct data in json format.
When i make normal link to edit page with plain ID (no encoding), remove decode in controller - everything works. What can be the problem. ? {{$client->name}} is correct 100% since it works as i say without encoding/decoding id
Thanks
Hi,
I´m encountering problems to encode/decode the ids of my models. It´s a lot of weird, because it doesn´t always happen.
I have a few screens to explain my problem:
I create a new document and check in the select, all the id's are encode:
Finally, go to dashboard, go again to create document, check the id´s encode (same as image 1) and now, the decode works well:
I don´t know what it´s happening....
This is all my config and uses:
'default' => 'main',
'connections' => [
'main' => [
'salt' => 'my-password-secret',
'length' => '12',
'alphabet' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',
],
'alternative' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
'alphabet' => 'your-alphabet-string',
],
],
public function create()
{
$users = User::select(['id', 'name'])->get();
foreach ($users as $user) {
$owners[Hashids::encode($user->id)] = $user->name;
}
return view('documents.create', ['owners' => $owners]);
}
{{ Form::select('user_id', $owners, null, ['class' => 'form-control']) }}
public function store(DocumentStore $request)
{
dump($request->all());
dump(Hashids::decode($request->user_id));
$user_id = Hashids::decode($request->user_id)[0];
$document = new Document();
$document->fill($request->all());
$document->user_id = $user_id;
$document->save();
return redirect()->route('documents.index')->with('message', 'Successfully created Document!');
}
Sorry for the long issue and thanks!!
On a blade view I receive an Id via URL, but the function "decode" in the controller returns an empty array.
The problem is not present on any other Controller (this is the only blade view we use, we use Laravel mainly as Restful API)
This is my function:
`public function getCI($add, $code) {
$add = Route::input('add');
$code = Route::input('code');
$add = Hashids::decode($add);
$code = Crypt::decrypt($code);
return View::make('users.ci',[
'add' => $add,
'code' => $code,
]);
}`
Is there a known bug or workaround?
Been searching for a while but no one seems to have experienced this sort of problem.
I want to generate more 5 lacs unique string with 6 alphanumeric long at time. Can I do with hashids ?
$arr = []; $query = []; for($i=1;$i<=500000;$i++){ $str = $hashids->encode($i); $query[] = [ "hashcode" => $str, ]; } foreach(array_chunk($query,5000) as $a){ \App\Model::insert($a); }
Here's I was questing on stackover flow and getting your Stackoverflow link
@vinkla how to define minimum hash length?
Argument 1 passed to Vinkla\Hashids\HashidsServiceProvider::registerFactory() must be an instance of Illuminate\Contracts\Foundation\Application
Since this commit Lumen Application (now version 5.2) does not implement the contract required anymore.
laravel/lumen-framework@365898c#diff-c9248b3167fc44af085b81db2e292837L36
I get this error on log whenever some commands are running in laravel,
the application is working as expected but in log it is getting created as soon as there is command fires
Where would I setup my custom salt or custom parameters that I want to use as default, such as minimum length?
Thanks!
I tried to install the package on a fresh laravel 5.1.45 installation and have this error:
Problem 1 - Installation request for vinkla/hashids ^3.0 -> satisfiable by vinkla/hashids[3.0.0]. - Conclusion: remove laravel/framework v5.1.45 - Conclusion: don't install laravel/framework v5.1.45 - vinkla/hashids 3.0.0 requires illuminate/contracts 5.2.* || 5.3.* -> satisfiable by illuminate/contracts[v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4]. - don't install illuminate/contracts v5.2.0|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.19|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.21|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.24|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.25|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.26|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.27|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.28|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.31|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.32|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.37|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.43|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.45|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.6|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.2.7|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.3.0|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.3.16|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.3.23|don't install laravel/framework v5.1.45 - don't install illuminate/contracts v5.3.4|don't install laravel/framework v5.1.45 - Installation request for laravel/framework (locked at v5.1.45, required as 5.1.*) -> satisfiable by laravel/framework[v5.1.45].
am using current system time to make sure the created ids never collide
system('date +%s%N')
but recently the lib stopped working and started to give empty string instead, ex.
Hashids::encode(1508058326172617500) // 20 digits
will give ""
, but if we reduced the number
Hashids::encode(150805832) // 9 digits
it will work "bzeoONEa"
any reason why this is happening ?
btw testing the same long number on https://codepen.io/ivanakimov/pen/bNmExm works
I found that you have already updated requirement to php 7+ and supported Auto-Discovery. However, the the version of Laravel is 5.3.*. I am testing Laravel 5.5 dev version. It is possible to support it? Thanks.
Hello
do you have any tips on how is to safety generate the 'salt'?
Thank you
There's some problem when using with Lumen 5.2, here's the error it's throwing:
[ErrorException]
Argument 1 passed to Vinkla\Hashids\HashidsServiceProvider::registerFactory() must be an instance of Illuminate\Contracts\Foundation\Application, i
nstance of Laravel\Lumen\Application given, called in /Users/User/Projects/Code/lumen/vendor/vinkla/hashids/src/HashidsServiceProvider.php on line 6
4 and defined
Hello, how i get a minimum hashid size using this?
I see that hashids has a option for this (padding), but i don´t see a "native" option in this bridge. Its possible?
I installed the package. Registered the service provider and boom
Allowed memory size of xxx bytes exhausted (tried to allocate xxx bytes)
It's not based on usage but just registering the provider does that.
Hello!
In laravel 5.3 i had custom connections with custom alphabet "0123456789abcdef"
Since the new version of hashids has alphabet length check, i must stay on prev version 2.4 which doesn't allow me to update it to laravel 5.4:
vinkla/hashids 2.4.0 requires illuminate/support 5.1.* || 5.2.* || 5.3.*
hi , i cant used package in lumen . can help me ?
We are hashing ID's of objects and save them in the database. Now we got 2 same hashes encoded by Hashids::encode(1290)
and Hashids::encode(1302)
now those same numbers encode in the following hash: 3GWpmbk4xdzJn4z
Now the package delivers a decode function: Hashids::decode('3GWpmbk4xdzJn4')
and it gives back an array with 1 item that has 1302. This matches the 2nd number encoded but not the first? It should either decode in both numbers, or the hash should be different for the numbers.
Any clue how this is possible or is this the behavier is need to have? We are using version v1.1.0 since the project is still running in laravel 5.1 and needs to be upgraded soon.
Edit* For version 3.3.0 the problem has been solved.
Why has the support for 5.6 been dropped? 5.6 has not reached end-of-life just yet and it will prevent a lot of people from using the package.
Hi,
We love the package, and use it in a rather high traffic REST API. I'll admit what I'm about to explain is a corner case, albeit one that burned us bad, and so I'd like to make a suggestion for a minor but important change.
We're running two production projects, a REST API and a legacy MVC web application. We upgraded the web application to Laravel 5.4, and in doing so learned the version of this package we were running only supported versions of Laravel 5.3 and earlier. However upon upgrading we learned the latest version only supported PHP 7. Because we're still running PHP 5.6, we concluded the practical solution would be to simply use the hashids/hashids package directly in the web application.
Due to legacy reasons our REST API just so happens to pass an encoded string over to the web application, meaning strings encoded by vinkla/hashids would be decoded by hashids/hashids. No big deal since vinkla/hashids ultimately relies upon hashids/hashids to do the encoding/decoding.
Even so, obviously out of paranoia we confirmed both packages were encoding and decoding the same strings in identical fashion. However just this morning I suddenly noticed this was no longer the case, and hashids/hashids was no longer able to decode strings encoded by vinkla/hashids.
This is a pretty big deal (to us), and so I scrambled to figure out what changed. Finally it dawned on me that at some point somebody published the vendor config packages to config/
, and in doing so the config/hashids.php
file which was generated actually overrode the default empty salt, 0 length, and empty alphabet configuration settings defined in HashidsFactory.php
, using these instead:
'main' => [
'salt' => '123456%(45$#',
'length' => '15',
'alphabet' => 'abcdefghijklmnopqrstuvwxyz0123456789',
],
After returning these settings to the defaults, everything started working again. Therefore long story short, I'd like to suggest the default config/hashids.php
file simply define main
like so, therefore leaving it to the user to define these as desired:
'main' => [
'salt' => '',
'length' => 0,
'alphabet' => '',
],
Thanks again for the great package!
@vinkla why is it generating hashes like -pbb
and bp-r
, so with the -
-character. How to prevent this? I'm using Hashids::encode($db->id);
This just happen to day after I upgrade PHP from 5.6 to 7.0
The encode is working fine but decode seem to be a problem. Could you please guide me where should I look.
Thanks for the great package you have made.
When I try install I get next:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for vinkla/hashids ^3.3 -> satisfiable by vinkla/hashids[3.3.0].
- Conclusion: remove laravel/framework v5.2.45
- Conclusion: don't install laravel/framework v5.2.45
- vinkla/hashids 3.3.0 requires illuminate/contracts 5.5.* -> satisfiable by illuminate/contracts[v5.5.0, v5.5.16, v5.5.17, v5.5.2].
- don't install illuminate/contracts v5.5.0|don't install laravel/framework v5.2.45
- don't install illuminate/contracts v5.5.16|don't install laravel/framework v5.2.45
- don't install illuminate/contracts v5.5.17|don't install laravel/framework v5.2.45
- don't install illuminate/contracts v5.5.2|don't install laravel/framework v5.2.45
- Installation request for laravel/framework (locked at v5.2.45, required as 5.2.*) -> satisfiable by laravel/framework[v5.2.45].
Installation failed, reverting ./composer.json to its original content.
What I need to try?
I tried installing laravel-hashids
on a fresh Laravel 5.4 install but, when I try to publish the vendor configuration (or when I try to do anything else, really) I get the following exception:
[Symfony\Component\Debug\Exception\FatalErrorException]
parse error, expecting `';'' or `'{''
Composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0",
"dingo/api": "1.0.x@dev",
"hashids/hashids": "^2.0",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"vinkla/hashids": "3.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.0"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Jump\\": "app/",
"Tests\\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
Hey!
I am running this in a laravel database seeder and using Hashids::encode($todo->id) gives me Segmentation fault: 11 in the console when I seed the database.
However, if I add a big enough number to the ID it works, for example, Hashids::encode($todo->id + 100) works fine.
Can anyone explain why this is happening? I'd rather not add 100 to every ID
Thanks in advance
/Teapot
Pretty new to Laravel, and I am playing with this package to create random-looking IDs.
I installed the package, created the config file, moved the variables in my .env file, and created a facade. When I try to call Hashids::encode() in a controller, for testing, like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Hashids; // aliased in app.php like 'Hashids' => Vinkla\Hashids\Facades\Hashids::class,
class TestController extends Controller
{
public function index(Request $request)
{
$hashed = Hashids::encode(1);
echo ($hashed);
}
}
It just kills my server. When I call the encode() function on a string, the server stays up but $hashed is empty.
Did I miss something?
Could this be a PHP version issue? (Running PHP 5.6.27 locally)
Thank you!
This lib support salts?
// Reference : https://github.com/ivanakimov/hashids.php#installation
$hashids = new Hashids\Hashids('this is my salt');
Hi,
Following your example, I got a empty return.
Eg: Hashids::encode(4815162342); //returns empty
Couriously Hashids::encode(481516234); //returns the hashid
There is a limit on id length?
Thanks in advance
The config might be better if it was a little more descriptive. I can fill the salt with garbage, I get that but what are the other values supposed to look like. Length? Alphabet? Idk.
'main' => [
'salt' => 'your-salt-string',
'length' => 'your-length-integer',
'alphabet' => 'your-alphabet-string'
],
Hi
I have a strange behavior happening in my code, \Hashids::decode works on certain routes/controllers but on another route/controller it does not.
The variable is the same with the same value, any help in the right path would be appreciated.
Thanks.
I get the following error using PHP 7.1 and Laravel 5.4
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'syntax error, unexpected ':', expecting ';' or '{'' in vendor\vinkla\hashids\src\HashidsServiceProvider.php:121
The error presents itself when calling Hashids::encode
Being able to extend the Laravel validator to check hashids would be helpful to check for validity on POSTs
$this->validate($request, [
'hashid' => 'required|hashids',
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
Info on creating custom validation rules can be found here.
I'm personally doing it this way but I'm sure there's a more optimal method
Validator::extend('hashid', function ($attribute, $value, $parameters, $validator) {
if (is_array($value)) {
foreach ($value as $item) {
if (!is_int(array_first(\Vinkla\Hashids\Facades\Hashids::decode($item)))) {
return false;
}
}
} else {
if (!is_int(array_first(\Vinkla\Hashids\Facades\Hashids::decode($value)))) {
return false;
}
}
return true;
});
Currently migrating from L4 to L5. How do I make the hash generator return the same hashes?
Your version includes salt, this wasn't in the L4 version. I've tried settings salt to '', but it's not giving me the same hashes.
{"message":"Call to undefined function Hashids\\bcdiv()","status_code":500}
I am on PHP 7.0.9.
It seems like my server is missing php-bcmath
!
Hey @vinkla ,
is it possible to add Laravel 5.6
support for this package?
You would need to change the composer.json
file accordingly:
"require": {
"php": "^7.1.3",
"graham-campbell/manager": "^3.0",
"hashids/hashids": "^2.0",
"illuminate/contracts": "5.5.*|5.6.*",
"illuminate/support": "5.5.|5.6.*"
},
"require-dev": {
"graham-campbell/analyzer": "^2.0",
"graham-campbell/testbench": "^5.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^6.5|~7.0"
},
This should, hopefully, do the trick ;)
Would be awesome to update the package to the latest changes!
Cheers and thank you very much!
Maybe I'm missing something but shouldn't decode
method return a single integer instead of an array, since hash is considered to be unique?
I'm using Laravel 5.1. I'm instructed in the readme to run php artisan vendor:publish
. But when I do so I get "Nothing to publish." and the configuratin file has not been added to my config directory.
I´m trying to return the id of the element using -> $id = Hashids::decode($uuid);
But, it returns me a array of one element. Its possible to return only the "id" value from the decode? "example: '10' "
i try to hash string decode as the following but the return value is empty array []
Note: encode() work well .
get('/test', function(){
$hash = Hashids::decode('1LLb3b4ck');
return $hash;
});
Any Suggestions ?
I'm currently trying with below code:
$hashids = new \App\Library\Hashids\Hashids('ap_', 6, 'abcdefghij1234567890');;
$arr = [];
$query = [];
for($i=1;$i<=500000;$i++){
$str = $hashids->encode($i);
$query[] = [
"hashcode" => $str,
"short_url" => "http://localhost:8000/s/".$str,
"long_url" => "http://localhost:8000/survey/group=0/user_id={$i}"
];
}
foreach(array_chunk($query,5000) as $a){
\App\Model::insert($a);
}
and My database structure is:
table: links
id: AI(PK)
hashcode: (255)(unique)
but It's completely work with 300000 but does repeat string after 3,00,000.
Can I get solution of it?
The new release adds support for Laravel 5.4 and removes support for PHP 5.6.
However, the minimum supported PHP version in Laravel is 5.6.4 So, people who use PHP 5.6 are not able to use Hashids.
Wouldn't it be better to keep support for PHP 5.6 ?
Hi. First off, thanks for the package. I've been using it for years. Recently, when Laravel 5.5 came I upgraded and migrated a project over to it. Everything doesn't work until it does and there's an odd roundabout way about this. Let me explain.
I'm using Laravel as a backend to a React JS frontend connected via the GraphQL Api...
The problem is, when I'm trying to encode some things, it causes my server to crash.
Per se... encoding the ID of a User model. Hashids::encode($user->id)
will cause the server to crash(yes the ID exists when I dd
it), unless I try and encode some random string first... Hashids::encode('adsadadasd')
, make a request to the server, then comment out the random string encoding, then normal encoding will work again.
If I clear the cache, php artisan cache:clear
, the problem returns. I have to encode a random made up string manually, make a request to the server, have it cache, then I can use the same old code as before and it will work perfectly... until the cache is cleared again. Please help.
Edit: Narrowed the problem down further, when I clear the cache, if I try to encode a string with 2 characters or less, it crashes. I believe that's why the $user->id
won't encode, because it's 1 char long. I tried encoding '1', '12', '123', '1234'. It works from '123' going but for it to work for '12' or '1', I have to encode something 3 chars long first.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.