Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel UUID from SQL Server Database Errors

I am having issues with a Laravel application using an existing database where MS SQL UUIDs are used. My application has a customer:

class Customer extends Model
{
    protected $table = 'ERP.Customer';
    public $timestamps = false;
    protected $primaryKey = 'CustID';
    protected $keyType = 'string';

    protected $fillable = [
    'CustID',
    'SysRowID',
    'CustNum',
    'LegalName',
    'ValidPayer',
    'TerritoryID',
    'Address1',
    'Address2',
    'Address3',
    'City',
    'State',
    'Zip',
    'Country',
    'SalesRepCode',
    'CurrencyCode',
    'TermsCode',
    'CreditHold',
    'FaxNum',
    'PhoneNum',
    'CustomerType'
];

public function SalesTer()
{
    return $this->belongsTo(SalesTer::class,'TerritoryID', 'TerritoryID');
}

public function Shipments()
{
    return $this->hasMany(Shipment::class, 'CustNum', 'CustNum');
}

public function Equipments()
{
    return $this->hasMany(Equipment::class,'CustNum', 'CustNum');
}

    public function Customer_UD()
    {
         return $this->hasOne(Customer_UD::class,'ForeignSysRowID', 'SysRowID');
    }
}

Which (in the native ERP application) has a UD table which end users can used to customise the Customer entity:

class Customer_UD extends Model
{
    protected $table = 'ERP.Customer_UD';
    protected $primaryKey = 'ForeignSysRowID';
    public $timestamps = false;
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'ForeignSysRowID',
        'MakesCans_c',
        'MakesEnds_c',
        'Industry_c'
    ];

    public function Customer()
    {
        return $this->hasOne(Customer::class,'SysRowID', 'ForeignSysRowID');
    }
}

CustomerController:

 public function show($CustID)
{

    if(Customer::find($CustID))
    {
        $Customer = Customer::find($CustID);
        $Customer_UD = $Customer->Customer_UD()
            ->get();

        $Shipments = $Customer->Shipments()
            ->where('Voided', '0')
            ->get();
        $Equipments = $Customer->Equipments()
            ->with('Part') // load the Part too in a single query
            ->where('SNStatus', 'SHIPPED')
            ->get();
        return view('Customer.show', ['NoCust' => '0'], 
compact('Equipments', 'Customer','Shipments', 'Parts', 'Customer_UD'));

    }
    else
    {
        return view('Customer.show', ['NoCust' => '1']);
    }
}

The Customer has (for whatever reason) a CustID (which people use to refer to the customer) a CustNum (which is not used outside of the database and a SysRowID. The SysRowID is used to link the Customer table with the Customer_UD table.

An example row from Customer_UD is:

enter image description here

My issue is that when trying to return the UD fields along with the Customer fields I get an error:

SQLSTATE[HY000]: General error: 20018 Incorrect syntax near ''. 
[20018] (severity 15) [select * from [ERP].[Customer_UD] where [ERP]. 
[Customer_UD].[ForeignSysRowID] = '���_�X�O�Q׊3�^w' and [ERP]. 
[Customer_UD].[ForeignSysRowID] is not null] 

I thought it was odd, so I commended out the Customer_UD lines in the CustomerController and simply tried to display the Customer UUID field in the show blade:

SysRowID: {{$Customer->SysRowID}}

I get nothing, no errors but no data. I created a controller and index blade for the Customer_UD model and can display all of the Customer_UD database fields apart from the UUID field.

I don't actually want to display the UUID fields - but do need to use them to build the relationships. Can anyone help point me in the right direction?

like image 505
Richard Craddock Avatar asked Apr 09 '26 03:04

Richard Craddock


1 Answers

I found that adding:

'options' => [
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
],

To the database configuration in config\database.php resolved the issue.

like image 76
Richard Craddock Avatar answered Apr 10 '26 16:04

Richard Craddock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!