Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Selecting Postgres UUID's on Laravel

I have a table on Postgres that auto generates UUIDs, when I dd Customer::all(); on Laravel I get an array with "cs_id" => "d0402be5-e1ba-4cb2-a80c-5340b406e2c3" which is fine. When I loop or select one record with the only the cs_id the data it retuns 0,2,5 for the three records currently on the table which is incorrect data.

EDIT:

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

On laravel

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

dd($customerData['cs_id']);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

For some reason Eloquent messes up there.

just add a getter and use it whenever you need the cs_id

public function getGuid()
{
    return $this->attributes['cs_id'];
}
over 4 years ago · Santiago Trujillo Report

0

To use uuids auto-generated by the database, define your model as follows:

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
}

Then you can use all Eloquent's methods as you would with classic ids:

$customerData = Customer::findOrFail('d0402be5-e1ba-4cb2-a80c-5340b406e2c3');
over 4 years ago · Santiago Trujillo Report

0

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

to get the record matching that pk.

I'm assuming on top you have use App\Customer;

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!