【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blogpost_post_id' in 'field list'SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列 'blogpost_post_id'
【发布时间】:2018-10-23 09:43:19
【问题描述】:

我对在 Laravel 上使用数据透视表感到困惑。我已经阅读了几篇类似的帖子,但都没有成功。

我是一个完全的初学者。这是我的消息。

SQLSTATE[42S22]:找不到列:1054 未知列 '字段列表'中的 'blogpost_post_id'(SQL:插入到 blogpost_blogtag (blogpost_post_id, blogtag_tag_id) 值 (1, 2))

问题很简单。 Laravel 是在列名前添加表名,我不知道为什么。

Laravel 调用 blogpost_post_id 而列名只是 post_id Laravel 调用 blogtag_tag_id 而列的名称只是 tag_id

模型

Blogpost.php

public function blogtags(){

    return $this->belongsToMany('App\Blogtag');
}

博客标签.php

public function blogposts(){

    return $this->belongsToMany('App\Blogpost');
}

I've also declared in the **Postcategory.php** model the following

public function blogposts()
{
    return $this->hasmany('App\Blogpost');
}

表格:

博文表

public function up()
{
    Schema::create('blogposts', function (Blueprint $table) {
        $table->increments('post_id');
        $table->integer('post_editorid')->default(0);
        $table->string('post_title');
        $table->string('post_slug');
        $table->text('post_content');
        $table->integer('post_categoryid');
        $table->boolean('post_banned')->default(0);
        $table->integer('post_moderatorid')->default(0);
        $table->dateTime('post_bandate')->nullable($value = true);
        $table->string('post_picture')->nullable($value = true);
        $table->string('post_extlink')->nullable($value = true);
        $table->softDeletes();
        $table->timestamps();
    });
}

后分类表

public function up()
{
    Schema::create('postcategories', function (Blueprint $table) {
        $table->increments('cat_id');
        $table->string('cat_name')->unique();
        $table->timestamps();
    });
}

博客标签表

public function up()

{
    Schema::create('blogtags', function (Blueprint $table) {
        $table->increments('tag_id');
        $table->string('tag_kw')->unique();
        $table->timestamps();
    });
}

还有 PIVOT 表

公共函数 up()

{
    Schema::create('blogpost_blogtag', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id');
        $table->integer('tag_id');
        $table->timestamps();
    });
}

和控制器(存储)

公共函数存储(请求 $request) { //dd($request->all());

$this->validate($request, [
    'posttitle' => 'required|min:4|max:255|string',
    'postcontent' => 'required|min:30|max:3000|string',
    'postpict' => 'nullable|image',
    'post_categoryid' => 'required',
    'postlink' => 'nullable|max:255|string',
    'blogtags' => 'nullable',

]);

    if ($request->hasFile('postpict')) {

        $postpict = $request->postpict;
        $postpict_new_name = time().$postpict->getClientOriginalName();
        $postpict->move('uploads/postpicts/', $postpict_new_name);
        $picturl = 'uploads/postpicts/' . $postpict_new_name;
    }else{
        $picturl = NULL;
    }

        $blogpost = Blogpost::create([
            'post_title' => $request->posttitle,
            'post_content' => $request->postcontent,
            'post_categoryid' => $request->post_categoryid,
            'post_picture' => $picturl,
            'post_extlink' => $request->postlink,
            'post_slug' => str_slug($request->posttitle)

        ]);

$blogpost->blogtags()->attach($request->blogtags);

$messageposttitle = $request->posttitle;
$message = $messageposttitle . ' is successfully stored';
$title = '';
Toastr::success($message, $title, ["positionClass" => "toast-top-center"]);

return redirect()->back();

}

$blogpost->blogtags()->attach($request->blogtags);

我会很高兴知道我所犯的错误。

【问题讨论】:

  • 当您遇到错误时,您可以发布您尝试执行的代码吗? Laravel 应该在这里期望列名 blogpost_idblogtag_id。不知道你是如何得到blogpost_post_idblogtag_tag_id 的错误

标签: laravel pivot-table


【解决方案1】:

尝试使用自定义外键

Blogpost.php

    public function blogtags(){
       return $this->belongsToMany('App\Blogtag','blogpost_blogtag','post_id','tag_id');
    }

Blogtag.php

public function blogposts(){

    return $this->belongsToMany('App\Blogpost','blogpost_blogtag','tag_id','post_id');
}

【讨论】:

  • 它工作得很好,谢谢。我将阅读更多关于自定义外键的内容。非常感谢。
  • 很抱歉回答这个问题,但这可能对你有用。 Laravel 关系文档。这更详细地解释了这些方法,包括在这个答案中定义列:laravel.com/docs/5.6/eloquent-relationships
  • 您的评论非常好。它实际上是我正在阅读以加深这个主题的文档,你说它非常详细是对的:)
  • @Code-Me-A-Unicorn 欢迎您。你能把这个答案标记为正确答案吗?它将帮助我改善我的个人资料。发送
  • 完成!一开始没注意这个按钮,谢谢你的好意提醒
猜你喜欢
  • 1970-01-01
  • 2019-10-14
  • 2021-10-31
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多