尝试在 laravel 中使用字符串作为外键
Trying to use string as foreign key in laravel
我只是一个初学者,尝试在 laravel 中使用字符串作为外键,但在获取数据时出现此错误:-
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
)
在这里我从收到的消息和一组未读消息中获取用户联系人
public function get(){
$contacts = DB::table('received')
->where('user_id', Auth::user()->uuid)
->get();
$unreadIds = Message::select(\DB::raw('`from` as sender_id, count(`from`) as messages_count'))
->where('to', Auth::user()->uuid)
->where('read', false)
->groupBy('from')
->get();
$contacts = $contacts->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->friends_id)->first();
$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;
return $contact;
});
return response()->json($contacts);
}
收到Table:-
public function up()
{
Schema::create('received', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('user_id', 36);
$table->unsignedBigInteger('friends_id');
$table->string('list_no')->nullable();
$table->string('name');
$table->timestamps();
});
}
这是我获取未读消息数的地方:-
public function up()
{
Schema::table('messages', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->boolean('read')->after('to', 36)->default(false);
});
}
public function down()
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn('read');
});
}
留言table
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('from', 36);
$table->string('to', 36);
$table->text('text');
$table->string('list_no')->nullable();
$table->timestamps();
});
}
谁能帮我解决这个问题。
仔细查看 sql 错误:形成的查询最初使用“from”而没有任何 table 名称,因为您将“from”用作 列 名称,即 SQL 查询关键字。
在您的消息 table 迁移中尝试使用不同的列名称可能是“from_user”而不是“来自”并刷新迁移以反映数据库中的更改。
还要在代码中更改相关的列名。
注意 - 除了更改旧迁移,您还可以通过创建新迁移并添加 $table->renameColumn('from', 'from_user');
来更改列名称
我只是一个初学者,尝试在 laravel 中使用字符串作为外键,但在获取数据时出现此错误:-
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'read' in 'where clause' (SQL: select
from
as sender_id, count(from
) as messages_count frommessages
whereto
= d3c364bb-0982-46ba-869a-24dbb2c50aea andread
= 0 group byfrom
)
在这里我从收到的消息和一组未读消息中获取用户联系人
public function get(){
$contacts = DB::table('received')
->where('user_id', Auth::user()->uuid)
->get();
$unreadIds = Message::select(\DB::raw('`from` as sender_id, count(`from`) as messages_count'))
->where('to', Auth::user()->uuid)
->where('read', false)
->groupBy('from')
->get();
$contacts = $contacts->map(function($contact) use ($unreadIds) {
$contactUnread = $unreadIds->where('sender_id', $contact->friends_id)->first();
$contact->unread = $contactUnread ? $contactUnread->messages_count : 0;
return $contact;
});
return response()->json($contacts);
}
收到Table:-
public function up()
{
Schema::create('received', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('user_id', 36);
$table->unsignedBigInteger('friends_id');
$table->string('list_no')->nullable();
$table->string('name');
$table->timestamps();
});
}
这是我获取未读消息数的地方:-
public function up()
{
Schema::table('messages', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->boolean('read')->after('to', 36)->default(false);
});
}
public function down()
{
Schema::table('messages', function (Blueprint $table) {
$table->dropColumn('read');
});
}
留言table
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('from', 36);
$table->string('to', 36);
$table->text('text');
$table->string('list_no')->nullable();
$table->timestamps();
});
}
谁能帮我解决这个问题。
仔细查看 sql 错误:形成的查询最初使用“from”而没有任何 table 名称,因为您将“from”用作 列 名称,即 SQL 查询关键字。
在您的消息 table 迁移中尝试使用不同的列名称可能是“from_user”而不是“来自”并刷新迁移以反映数据库中的更改。
还要在代码中更改相关的列名。
注意 - 除了更改旧迁移,您还可以通过创建新迁移并添加 $table->renameColumn('from', 'from_user');