Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda