Hi everyone, hope y'all doing well, here's my concern. It's the first time I see something like this.
In my SQL Server database, I have a column called UserID which has GUID like D9865025-2994-4A9E-A714-4B48190F235D. When I attempt to retrieve it using either Eloquent or the Query builder, it returns to some characters like b"%P†Ù”)žJ§\x14KH\x19\x0F#]"'t get how it's converted or how do I get it back to it's original value.
After some search, someone suggested to cast the uniqueidentifier as a varchar. How can I do that with Eloquent?
When I use the query builder on my model I would like to convert the field. So I could be able to do User::find(1) and it casts like SELECT CAST(unique_id_column AS VARCHAR(36)) AS my_id for example
EDIT:
I've been able to safely retrieve the value with :
User::select([DB::raw('CAST(UserID AS VARCHAR(36)) AS UserID')])->where('ID', 1)->first()
The thing is that I have to write it everywhere, it's a bit complicated, hope someone can help
It's a uniqueidentifier, I managed to make a global scope which looks like the following :
<?php
namespace App\Scopes;
use Illuminate\Database\Query\Builder as BaseBuilder;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ScopeInterface;
class GuidScope implements ScopeInterface
{
public function apply(Builder $builder, Model $model)
{
$builder->select(['*', DB::raw('CAST(UserID AS VARCHAR(36)) AS UserID')]);
}
public function remove(Builder $builder, Model $model)
{
}
}
And a trait
<?php
namespace App\Scopes;
trait GuidTrait
{
public static function bootGuidTrait()
{
static::addGlobalScope(new GuidScope);
}
}
The thing is that the column output successfully on my CentOS server but it gives me this on my mac, looks like a driver issues, my freetds.conf are configured exactly the same way