Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dpakagarwal's avatar

Column not found

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.

0 likes
9 replies
Sinnbeck's avatar

Follow the naming convention of laravel or manually set all foreign keys

Example. Uppercase letter and singular form of the word

class Post 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';
    }
}

I suggest you watch the free laravel 8 from scratch series

dpakagarwal's avatar

@Sinnbeck in this

public function tags()
    {
        return $this->belongsToMany(BlogTag::class, 'blogpost_tags')->withTimestamps();
    }

i m just passing another pivoted model name as plural bcoz it getting error on singular table name convention

dpakagarwal's avatar

@Sinnbeck i had already given all codes that is required or giving me error, and also i have updated all models to camel case as you have mentioned

dpakagarwal's avatar

@Sinnbeck Column not found: 1054 Unknown column 'blog_tag_id' in 'field list' (SQL: insert into blogpost_tags (blog_tag_id, created_at, post_id, updated_at) values (1, 2022-01-26 07:06:21, 5, 2022-01-26 07:06:21))

now getting this error while inserting post, I m not getting from where it's taken blog_tag_id column.

SilenceBringer's avatar

@deepakagarwal following the convention, for blogTag model default foreign key will be blog_tag_id. you need to set it obviously in your relationship if you do not follow it

    public function tags()
    {
        return $this->belongsToMany(blogTag::class, 'blogpost_tags', 'post_id', 'tag_id')
			->withTimestamps();
    }

Please or to participate in this conversation.