2017-04-24 68 views
0

我在Laravel上使用我有一個Postgres自動生成UUID的表格,我得到一個數組,"cs_id" => "d0402be5-e1ba-4cb2-a80c-5340b406e2c3"這很好。當我循環或選擇一個只有cs_id的記錄時,它會爲表格中當前的三條記錄返回0,2,5,這是不正確的數據。在Laravel上選擇Postgres UUID

編輯:

CREATE TABLE customers 
(
    cs_id character varying(255) NOT NULL DEFAULT gen_random_uuid(), 
CONSTRAINT cs_customers_pkey PRIMARY KEY (cs_id), 
} 

在laravel

$customerData = Customer::where('cs_id','d0402be5-e1ba-4cb2-a80c-5340b406e2c3')->first(); 

dd($customerData['cs_id']); 
+0

請提供驗證碼 –

回答

2

出於某種原因,雄辯混亂那裏。

只需添加一個getter和當您需要的cs_id

public function getGuid() 
{ 
    return $this->attributes['cs_id']; 
} 
0

使用Customer::findOrFail('d0402be5-e1ba-4cb2-a80c-5340b406e2c3');

得到匹配PK記錄。

我假設在上面你有use App\Customer;

0

要使用的UUID自動生成的數據庫,定義你的模型按如下方式使用它:

class Customer extends Model 
{ 
    // rename the id column (optional) 
    protected $primaryKey = 'cs_id'; 

    // tell Eloquent that your id is not an integer 
    protected $keyType = 'string'; 

    // do NOT set $incrementing to false 
} 

然後你可以使用所有Eloquent的方法與您使用經典ID相同:

$customerData = Customer::findOrFail('d0402be5-e1ba-4cb2-a80c-5340b406e2c3');