Skip to content
Advertisement

Trying to use string as foreign key in laravel

I am just a beginner and trying to use string as foreign key in laravel but getting this error while fetching the data:-

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘read’ in ‘where clause’ (SQL: select from as sender_id, count(from) as messages_count from messages where to = d3c364bb-0982-46ba-869a-24dbb2c50aea and read = 0 group by from)

In here I am fetching users contacts from received and a group of unread message count

JavaScript

Received Table:-

JavaScript

This is from where i am fetching unread message count:-

JavaScript

Here’s Message table

JavaScript

Can anyone please help me through this.

Advertisement

Answer

Looking closely at the sql error: the query formed is using “from” initially without any table name because you have used “from” as a column name which is SQL keyword for making queries.

Try a different column name may be “from_user” instead of “from” in your message table migrations and refresh the migrations to reflect changes in DB.

Also make the relevant column name changes in the code as well.

Note- Instead of changing old migration, you can also change the column name by creating a new migration and adding $table->renameColumn('from', 'from_user');

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement