【发布时间】: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_id和blogtag_id。不知道你是如何得到blogpost_post_id和blogtag_tag_id的错误
标签: laravel pivot-table