首页 文章

SQLSTATE [42S22]:找不到列:1054未知列'blogpost_post_id' in 'field list'

提问于
浏览
0

我很难在Laravel上使用数据透视表 . 我读过几篇类似的帖子却没有成功 .

我是一个完全的初学者 . 这是我的信息 .

SQLSTATE [42S22]:找不到列:1054未知列'blogpost_post_id' in 'field list'(SQL:插入 blogpost_blogtagblogpost_post_idblogtag_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');
}

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');
}

表:

blogpost table

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();
    });
}

Postcategories table

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

Blogtags table

public function up()

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

And the PIVOT table

公共功能up()

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

和控制器(商店)

public function store(Request $ 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);

我很乐意了解我所犯的错误 .

1 回答

  • 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');
    }
    

相关问题