最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Laravel 10 Eloquent. Morph fields type error - Stack Overflow

programmeradmin9浏览0评论

I`m using postgres 17.2

I have transactions table with morph columns entity_type and entity_id. When i use whereHasMorph i got a type error with entity_id because users table id is bigint

Example:

$query->whereHas('user', function (Builder $query) use ($dto) {
                $query->whereRaw("id = ?", [$dto->sender])
                    ->orWhereRaw("username = ?", [$dto->sender]);
            });
            $query->whereHasMorph('entity', [EntityTypeEnum::USER->value], function (Builder $query) use ($userId) {
                $query->whereRaw("id = ?", [$userId]);
            });
            return;

Output Error:

SQLSTATE[42883]: Undefined function: 7 ERROR:  operator does not exist: character varying = bigint\n
      LINE 1: ...users" where "payment"."transactions"."entity_id" = "account...\n
                                                                   ^\n
      HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
      

What to do in this situation, how to fix the error in types without using raw sql

Chat gpt dont give me any working solutions

I`m using postgres 17.2

I have transactions table with morph columns entity_type and entity_id. When i use whereHasMorph i got a type error with entity_id because users table id is bigint

Example:

$query->whereHas('user', function (Builder $query) use ($dto) {
                $query->whereRaw("id = ?", [$dto->sender])
                    ->orWhereRaw("username = ?", [$dto->sender]);
            });
            $query->whereHasMorph('entity', [EntityTypeEnum::USER->value], function (Builder $query) use ($userId) {
                $query->whereRaw("id = ?", [$userId]);
            });
            return;

Output Error:

SQLSTATE[42883]: Undefined function: 7 ERROR:  operator does not exist: character varying = bigint\n
      LINE 1: ...users" where "payment"."transactions"."entity_id" = "account...\n
                                                                   ^\n
      HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
      

What to do in this situation, how to fix the error in types without using raw sql

Chat gpt dont give me any working solutions

Share Improve this question asked Mar 15 at 3:17 EyeVersEyeVers 1 1
  • The character varying = bigint in the error tells us that somewhere in the query a text value is being compared with a number, which it does not like. My guess is that entity_id is the bigint value and the account... something is text? – JorisJ1 Commented Mar 16 at 13:44
Add a comment  | 

1 Answer 1

Reset to default 0

Your issue arises because in your transactions table, entity_id is likely stored as a VARCHAR (or TEXT), whereas the id column in the users table is of type BIGINT. PostgreSQL does not automatically cast VARCHAR to BIGINT when comparing them in a WHERE clause.

发布评论

评论列表(0)

  1. 暂无评论