I have three tables namely: posts, category, and tags respectively. and also created pivot table with their relationships as follows:
Posts migration:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('subtitle');
$table->string('slug');
$table->text('body');
$table->boolean('status')->default(1);
$table->integer('posted_by')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
blog category migration:
Schema::create('blog_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
blog tags migration:
Schema::create('blog_tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
blog category_posts migration (pivot):
Schema::create('blogcategory_posts', function (Blueprint $table) {
$table->unsignedBigInteger('post_id')->index();
$table->unsignedBigInteger('category_id')->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
blog tag_posts migration (pivot):
Schema::create('blogpost_tags', function (Blueprint $table) {
$table->unsignedBigInteger('post_id')->index();
$table->unsignedBigInteger('tag_id')->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
posts model :
class posts extends Model
{
use HasFactory;
public function tags()
{
return $this->belongsToMany(blogTag::class, 'blogpost_tags')->withTimestamps();
}
public function categories()
{
return $this->belongsToMany(blogCategory::class, 'blogcategory_posts')->withTimestamps();
}
public function getRouteKeyName()
{
return 'slug';
}
}
blogtag model:
class blogTag extends Model
{
use HasFactory;
public function posts()
{
return $this->belongsToMany(posts::class, 'blogpost_tags');
}
}
blogcategory model:
class blogCategory extends Model
{
use HasFactory;
protected $fillable = ['name','slug'];
public function posts()
{
return $this->belongsToMany(posts::class, 'blogcategory_posts');
}
}
blogcontroller file:
public function insertBlog(Request $request){
$imageName = '';
if ($request->hasFile('image')) {
$imageName = $request->image->store('public');
}
$post = new posts();
$post->title = $request->title;
$post->image = $imageName;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = $request->status;
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect()->route('admin.blog.listBlog')->with('success', 'Post created successfully.');
}
i m getting error on $post->tags()->sync($request->tags); line as
Column not found: 1054 Unknown column 'blogpost_tags.posts_id' in 'where clause' (SQL: select * from `blogpost_tags` where `blogpost_tags`.`posts_id` = 9)
as i have mentioned post_id but getting as posts_id
help me out if i m not getting any solution related to this scenario.