[英]Why am I getting a column not found error when trying to return a collection model based on user ID?
I'm trying to return all Threads that a given User has participated in.我正在尝试返回给定用户参与的所有线程。
The endpoint accepts a userId and supposed to return a collection of thread models.端点接受一个 userId 并且应该返回一个线程模型的集合。
However, I keep getting this error when executing the controller action.但是,在执行 controller 操作时,我不断收到此错误。 It's looking for a
message_id
column but I don't have that defined on the thread
table or on any table, for that matter - making this a weird error.它正在寻找一个
message_id
列,但我没有在thread
表或任何表上定义它,就此而言 - 使这是一个奇怪的错误。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'thread.message_id' in 'where
clause' (SQL: select * from `thread` where `thread`.`message_id` = 2 and
`thread`.`message_id` is not null)"
I believe there might be something off with how I'm linking the tables but I'm not entirely sure.我相信我链接表格的方式可能有些问题,但我并不完全确定。 I'd assume the
message
table's column thread_id
should reference the id
column on the thread
table which's what I thought I was doing in the message
migration below.我假设
message
表的列thread_id
应该引用thread
表上的id
列,这就是我认为我在下面的message
迁移中所做的。
What am I doing wrong and how can I fix this?我做错了什么,我该如何解决?
Here's users migration:这是用户迁移:
Schema::create('users', function (Blueprint $table) {
$table->id('id');
$table->string('email')->unique();
$table->string('full_name');
$table->string('password');
});
Here's thread migration:这是线程迁移:
Schema::create('thread', function (Blueprint $table) {
$table->id();
$table->string('title');
});
Here's message migration:这是消息迁移:
Schema::create('message', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('thread_id');
$table->string('body');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('thread_id')
->references('id')
->on('thread')
->onDelete('cascade');
});
controller action: controller 动作:
public function getUserThreads($userId) {
$userParticipatedThreads = Message::findOrFail($userId);
return $userParticipatedThreads->thread;
}
message model:消息 model:
public function thread() {
return $this->hasMany(Thread::class);
}
endpoint:端点:
[GET] http://127.0.0.1:8000/api/getUserThreads/2
Route::get('getUserThreads/{userId}', [ThreadController::class, 'getUserThreads']);
Your thread
relationship on your Message class is looking for the message_id
, since that's the default way the hasMany relationship works.您的 Message class 上的
thread
关系正在寻找message_id
,因为这是 hasMany 关系的默认工作方式。 You'll need to override the column that it's basing the relationship off of.您需要覆盖它所基于的关系的列。
public function thread() {
return $this->hasMany(Thread::class, 'id', 'thread_id');
}
However, since it looks like the message belongs to one single thread (each message has a thread_id), then you actually want belongsTo
instead但是,由于看起来消息属于一个单独的线程(每条消息都有一个 thread_id),那么您实际上想要
belongsTo
代替
public function thread() {
return $this->belongsTo(Thread::class);
}
To answer the question;回答问题;
Why am I getting a column not found error when trying to return a collection model based on user ID?
为什么在尝试根据用户 ID 返回集合 model 时出现未找到列错误?
Because your thread
table doesn't have a message_id
field defined on it.因为您的
thread
表上没有定义message_id
字段。
Schema::create('thread', function (Blueprint $table) {
$table->id();
$table->string('title');
});
A Message
belongs to a Thread
but you seem to have that relationship inverted.一条
Message
属于一个Thread
,但您似乎将这种关系颠倒了。
On your Thread
model, define a relationship to the Message
model:在您的
Thread
model 上,定义与Message
model 的关系:
public function messages()
{
return $this->hasMany(Message::class);
}
Then you can query for the existance of some message by user:然后您可以通过用户查询某些消息的存在:
$messages = Thread::whereHas('messages', function ($query) use ($userId) {
$query->where('user_id', $userId);
})->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.