Elvis Reis
Elvis Reis

Reputation: 305

How to use primary key in laravel?

i'm having trouble using incrementing even if i set it to false laravel tries to use a numeric key

using postgree

$atividade = Atividade::whereServicoId('14.01')->first();

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer LINE 1: select * from "servicos" where "servicos"."id" in (11) ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "servicos" where "servicos"."id" in (11))

<?php

class Atividade extends Model
{
    use HasFactory;

    protected $with = ['servico'];
    protected $fillable = ['id', 'descricao'];
    public $incrementing = false;

    /**
     * @return BelongsTo
     */
    public function servico(): BelongsTo
    {
        return $this->belongsTo( Servico::class, 'servico_id', 'id');
    }
}

<?php

class Servico extends Model
{
    use HasFactory;

    protected $fillable = ['id', 'descricao'];
    protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
    public $incrementing = false;

}

Upvotes: 0

Views: 238

Answers (1)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

I think this will helps you

$invoices = Atividade::with(array('servico' => function($query) {
    $query->where('id', '=', '14.01');
}))->first();

Upvotes: 1

Related Questions