Reputation: 23
I'm stumped with the use of Pivot Table on Laravel. I've read several similar posts with no success.
I'm a complete beginner. Here's the message I have.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blogpost_post_id' in 'field list' (SQL: insert into blogpost_blogtag
(blogpost_post_id
, blogtag_tag_id
) values (1, 2))
The issue is pretty simple. Laravel is adding the name of the table before the name of the column, and I can't find out why.
Laravel calls blogpost_post_id
whereas the name of the column is simply post_id
Laravel calls blogtag_tag_id
whereas the name of the column is simply 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
public function up()
{
Schema::create('blogpost_blogtag', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('tag_id');
$table->timestamps();
});
}
And the controller (store)
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);
I'll be happy to learn about the mistake I've done.
Upvotes: 1
Views: 1646
Reputation: 377
Try to use custom foreign-key
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');
}
Upvotes: 3