0

I have two models with the same ID key that is a Varbinary(18) datatype in SQL Server. I am having trouble finding a way to deal with this datatype. I believe the main issue is that eloquent converts this data to a string. The database is existing with other applications depending on it so it cannot be modified.

Example:

TableFoo - first()->ID = 0x1234

TableBar - has an entry with ID 0x1234

$foo = App\TableFoo::first()->ID;
echo $foo 

//this echoes 1234 (removes the 0x)
// which makes the below return NULL

$bar = App\TableBar::where('ID', $foo)->get();

I have attempted to manually just add the 0x back on to the ID but this also does not find the ID in the second table and I believe this is because it is sending the value as a string.

If I manually do the query:

SELECT * FROM TableBar where ID=0x1234    //1 result returned
SELECT * FROM TableBar where ID='0x1234'  //0 results returned (how I think eloquent is trying to do the query)

I have seen in the docs that I can cast a datatype but none of the available types seem like they will help. This problem is also breaking Eloquent relationships.

My main question: is there a way to easily work with this datatype within Eloquent models or should I quit banging my head against a wall and go with a more manual query approach?

4

0 回答 0