【问题标题】:Column not found: 1054 Unknown column 'taggables.tags_model_id' in 'field list' laravel eloquent未找到列:1054 未知列“字段列表”中的“taggables.tags_model_id”laravel 雄辩
【发布时间】:2020-04-29 20:30:31
【问题描述】:

我将实现一个查询来检索我所有的博客及其标签。我正在使用 Laravel Eloquent 多态关系,但我有一个错误是 Column not found: 1054 Unknown column 'taggables.tags_model_id' in 'field list'。我在 laravel 和我的 SQL 中的所有代码如下所示:

我的桌子:


create table tags
(
    id         int auto_increment
        primary key,
    name       varchar(200)                        null,
    created_at timestamp default CURRENT_TIMESTAMP not null,
    updated_at timestamp                           null,
    deleted_at timestamp                           null
);


create table taggables
(
    id            int auto_increment
        primary key,
    tag_id        int                                 null,
    taggable_type varchar(512)                        null,
    taggable_id   int                                 null,
    created_at    timestamp default CURRENT_TIMESTAMP not null,
    updated_at    timestamp                           null,
    deleted_at    timestamp                           null,
    constraint fk23
        foreign key (tag_id) references tags (id)
            on update cascade on delete cascade
);

create index taggables_tag_id_index
    on taggables (tag_id);


create table blog
(
    id          int auto_increment
        primary key,
    title       varchar(200)                        null,
    passage     text                                null,
    author      varchar(200)                        null,
    category    varchar(200)                        null,
    img_url     varchar(200)                        null,
    created_at  timestamp default CURRENT_TIMESTAMP not null,
    updated_at  timestamp                           null,
    deleted_at  timestamp                           null,
    user_id     int                                 not null,
    category_id int                                 null,
    constraint fk18
        foreign key (user_id) references users (id)
            on update cascade on delete cascade,
    constraint fk19
        foreign key (category_id) references blog (id)
            on update cascade on delete cascade
);

create index blog_index
    on blog (category_id);

create index blog_users_index
    on blog (user_id);


雄辩的模型


class BaseModel extends Model
{
    protected $table;
    protected $primaryKey;


    use SoftDeletes;

}

class BlogModel extends BaseModel
{

    protected $table = 'blog';
    protected $primaryKey = 'id';


    public function tags()
    {
        return $this->morphToMany(TagsModel::class,"taggable");

    }
}


class TagsModel extends BaseModel
{

    protected $table = 'tags';
    protected $primaryKey = 'id';

    public function blog()
    {
        return $this->morphedByMany(BlogModel::class,"taggable");
    }

}


当我调用这个查询时,结果是一个空数组

 public function getItemsWithTags(array $attr)
    {
        return BlogModel::find(1)->tags;
    }

非常感谢。

【问题讨论】:

  • 你的表名tag,但是你的模型名TagModel。怎么可能??
  • 亲爱的@A.ANoman,我对所有其他表都这样做了,它确实有效

标签: php mysql laravel eloquent polymorphic-associations


【解决方案1】:

Laravel 的多态关系默认使用反射来确定键列的名称。因为你的模型叫做TagModel,Laravel 假设外键是tag_model_id

    // Illuminate\Database\Eloquent\Model
    public function getForeignKey()
    {
        return Str::snake(class_basename($this)).'_'.$this->getKeyName();
    }

您可以通过将正确的外键显式传递给 morphedByMany 方法来解决此问题:

public function tags()
{
    return $this->morphedByMany(TagsModel::class, 'taggable', null, 'tag_id');
}

【讨论】:

  • 这个错误显示给我PHP Fatal error: Uncaught Error: Class 'taggable' not found。我将 tags() 放在我的 BlogModel 类中,但我将此方法更改为 php public function tags() { return $this->morphedByMany(TagsModel::class,'taggable', null, 'tag_id'); } 并且它可以工作,但结果是空数组
  • 哎呀,我错过了第一个参数:)
【解决方案2】:

你不能做像 BaseModel 这样的中级育儿课程。因为它在具有 $table 属性的 Laravel 中不起作用,请尝试这样做:

class BlogModel extends Model
{
    use SoftDeletes;

    protected $table = 'blog';

    protected $primaryKey = 'id';

    public function tags()
    {
        return $this->morphToMany(TagsModel::class,"taggable");
    }
}


class TagsModel extends Model
{
    use SoftDeletes;

    protected $table = 'tags';

    protected $primaryKey = 'id';

    public function blog()
    {
        return $this->morphedByMany(BlogModel::class,"taggable");
    }
}

【讨论】:

  • Column not found: 1054 Unknown column 'taggables.tags_model_id' in 'field list' 这是错误
【解决方案3】:

试试这个例子,你需要在 eloquent 方法中引用“标签”。

BlogModel::with('tags')->find(1);

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 2013-05-26
    • 2021-10-24
    • 2021-12-03
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多