PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] "Unknown column 'tag_tag_id' in 'field list'" error on Many To Many Polymorphic Relations

 January 20, 2022     laravel, laravel-5.5, php     No comments   

Issue

I have a Tag model like this :

class Tag extends Model
{
    protected $primaryKey = 'tag_id';

    protected $fillable = ['name'];

    public function products()
    {
        return $this->morphedByMany(Product::class, 'taggable');
    }
}

And On the other hand there is a Product model like this :

class Product extends Model
{
    protected $primaryKey = 'product_id';
    protected $fillable = ['code', 'title', 'description'];

    public function tags()
    {
        return $this->morphToMany(\Modules\Tag\Entities\Tag::class, 'taggable');
    }

}

As You can see there is a many mo many polymorphic relations between them . But each time I want to store a product that have some tags like this :

public function store(ProductFormRequest $request)
{
    $newProduct = Product::create($request->all());

    if ($request->has('tags')) {
        $newProduct->tags()->sync($request->get('tags'));
    }

    return $this->item($newProduct, new ProductTransformer);
}

I got this error :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tag_tag_id' in 'field list' (SQL: select `tag_tag_id` from `taggables` where `taggable_id` = 4 and `taggable_type` = Modules\\Product\\Entities\\Product)

What is problem and How can I solve that ?


Solution

Many To Many Polymorphic Relations:

products
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

As you can see, the default "tag ID" column in taggables table is tag_id. This means, the name of the table in the singular + _ + primary key name. Since you specify your primary key as tag_id, the query is searching for tag_tag_id.

You have two solutions:

1) Stick to the convention and use the default id as your primary keys.

2) Pass a parameter to morphToMany specifying that the primaryKey is tag_id. Here you have all the parameters that accepts the morphToMany method: MorphToMany API.

I'm not sure which one is the correct (I think is $relatedPivotKey), but you should try ($foreignPivotKey, $relatedPivotKey, $parentKey or $relatedKey). Pass the default values for the ones you are not changing.

Something like this: $this->morphToMany(\Modules\Tag\Entities\Tag::class, 'taggable', null, null, 'tag_id');



Answered By - azeós
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing