![](/img/trans.png)
[英]Fail generate UUID datatype (uniqueidentifier) from sql server using laravel
[英]Laravel UUID from SQL Server Database Errors
我在使用使用MS SQL UUID的現有數據庫的Laravel應用程序中遇到問題。 我的應用程序有一個客戶:
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');
}
}
哪個(在本機ERP應用程序中)具有UD表,最終用戶可以使用該表自定義Customer實體:
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']);
}
}
客戶有一個(出於某種原因)CustID(人們用來指代客戶),一個CustNum(不在數據庫外部使用)和SysRowID。SysRowID用於將Customer表與Customer_UD表鏈接。
Customer_UD的示例行是:
我的問題是,當嘗試返回UD字段以及Customer字段時,出現錯誤:
SQLSTATE[HY000]: General error: 20018 Incorrect syntax near ''.
[20018] (severity 15) [select * from [ERP].[Customer_UD] where [ERP].
[Customer_UD].[ForeignSysRowID] = '���_�X�O�Q3�^w' and [ERP].
[Customer_UD].[ForeignSysRowID] is not null]
我以為這很奇怪,所以我贊揚了CustomerController中的Customer_UD行,只是嘗試在顯示刀片中顯示Customer UUID字段:
SysRowID: {{$Customer->SysRowID}}
我什么也沒有,沒有錯誤但沒有數據。 我為Customer_UD模型創建了控制器和索引刀片,並且可以顯示除UUID字段之外的所有Customer_UD數據庫字段。
我實際上並不想顯示UUID字段-但確實需要使用它們來建立關系。 誰能幫助我指出正確的方向?
我發現添加:
'options' => [
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
],
到config \\ database.php中的數據庫配置解決了該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.