简体   繁体   English

为什么在尝试根据用户 ID 返回集合 model 时出现未找到列错误?

[英]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.

相关问题 当我尝试获取用户ID时,它返回null值 - It return null value when i am trying to get user id 为什么在尝试加载模型时出现应用程序错误? - Why am I getting Application error while trying to load a model? 为什么我在尝试使用phpmailer发送电子邮件激活新用户帐户时收到此错误 - Why am i getting this error when trying to send email for activation of new user account with phpmailer Codeigniter:“错误1048列'customer_id'不能为空”为什么会出现此错误? - Codeigniter: “Error 1048 Column 'customer_id' cannot be null” Why am I getting this error? 为什么我在尝试查找特定用户时从 laravel 的数据库中获取 null 值? - why am i getting null value from my database in laravel when trying to find specific user? 为什么会出现此错误? 致命错误:找不到类“ TestCase” - Why am I getting this error? Fatal error: Class 'TestCase' not found 嵌入式框架:当文件存在时,为什么会出现找不到资源的错误? - Recess framework: Why am I getting a resource not found error when the file exists? 尝试在CakePHP中显示表单时,为什么会出现“调用未定义方法”错误? - Why am I getting a “call to undefined method” error when trying to display a form in CakePHP? 为什么在尝试关闭 zip 文件时出现错误“无法创建临时文件”? - Why am I getting error "Failure to create temporary file" when trying to close a zip file? 为什么会出现“找不到工匠类”错误? - Why am I getting the “Class 'Artisan' not found” error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM