I have a users table that doesn't use an auto increment primary key, instead it uses a binary uuid primary key. I set up my custom model to interact with my table however, I'm having trouble trying to find records by using ::find()
because for a case like this, this uuid needs to searched by using HEX()
and UNHEX()
mysql functions. How to I override this and have whatever is in ::find()
to be hexed. The name of the model is Player.
So if I was to try to find a user with a uuid of 9d823b9eec224cbea10b69bec2c5adef
, I cannot find them by doing:
Player::find('9d823b9eec224cbea10b69bec2c5adef')
since the uuid needs to be unhexed.
I've tried Player::find("UNHEX('9d823b9eec224cbea10b69bec2c5adef')");
with no results.
Here's my model so far:
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
}
The datatype for uuid is binary(16)
Update
I've got it to work by using Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"));
however this is a lot to write every time I need to look up a user.
Is there any way I can have the parameter for ::find()
always run through DB::raw("UNHEX('uuid')")
or be passed through the function hex2bin()
?
I am 100% certain I will always be using UUID so I want to override ::find()
, not create a new method.