【问题标题】:morphToMany column not found未找到 morphToMany 列
【发布时间】:2021-07-05 22:55:06
【问题描述】:

我遵循 laravel 文档中的教程,但似乎教程没有完整解释参数 morphToMany 以明确确定函数应指向的内容。

SQLSTATE[42S22]:未找到列:1054 未知列 '字段列表'中的'tagables.tagables_id'(SQL:选择tags.*, tagables.tagables_id 作为pivot_tagables_id, tagables.tag_tag_id 作为pivot_tag_tag_id, tagables.tagables_type 作为pivot_tagables_type 来自tags 内部 在tags.tag_id 上加入tagables = tagables.tag_tag_id tagables.tagables_id = 1 和 tagables.tagables_type = 应用\模型\漫画)

媒体表

    Schema::create('media', function (Blueprint $table) {
         $table->mediumIncrements('media_id)->nullable(false);
         $table->string('title', 255);
    });

标签表//标签内的一行(步行)

    Schema::create('tags', function (Blueprint $table) {
        $table->mediumIncrements('tag_id');
        $table->string('tag_name', 255);
    });
   

标签表

    Schema::create('tagables', function (Blueprint $table) {
        $table->unsignedMediumInteger('tag_id')->nullable(false);
        $table->unsignedMediumInteger('tagable_id')->nullable(false);
        $table->string('tagable_type', 255)->nullable(false);
    });

漫画模型

public function tags()
{
    return $this->morphToMany(Tag::class, 'tagable');
}

漫画控制器

   // insert new comic
    $comic = Comic::create([
        'title'         => 'Doraemon',
    ]);
   
   // Insert into tagables table with current comic id and bind to tag id 1 
   which is walking

   $comic->tags->create([
       'tag_id' => 1
   ]);

插入媒体成功,插入tagables表失败。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    我在您的代码中发现了一些错误。请尝试重写此代码。您的代码有一个工作示例。我帮你查过了

    2021_04_10_073813_create_media_table.php

     Schema::create('media', function (Blueprint $table) {
            $table->id();
            $table->string('name', 255);
            $table->timestamps();
        });
    

    2021_04_10_073837_create_tags_table.php

     Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('name', 255);
            $table->timestamps();
        });
    

    2021_04_10_073856_create_taggables_table.php

    Schema::create('taggables', function (Blueprint $table) {
            $table->id();
            $table->unsignedMediumInteger('tag_id')->nullable(false);
            $table->unsignedMediumInteger('taggable_id')->nullable(false);
            $table->string('taggable_type');
            $table->timestamps();
        });
    
    class Media extends Model
    {
        protected $guarded = [];
    
        public function tags()
        {
            return $this->morphToMany(Tag::class, 'taggable');
        }
    }
    
    class Tag extends Model
    {
        protected $guarded = [];
    
        public function medias()
        {
            return $this->morphedByMany(Media::class, 'taggable');
        }
    }
    

    最后在某个控制器中

    $media = Media::create([
                'name'         => 'media name1',
            ]);
    
            $tag = $media->tags()->create([
                'name' => 'tag name1',
    
            ]);
    

    【讨论】:

    • 我已经通过查看其他网站修复了,在漫画控制器中,我显式地在 morphToMany 中填充参数。 public function tags(){Tag::class, 'tagable', 'tagables', null, 'tag_id');。现在我有其他问题,我创建了我的新问题stackoverflow.com/questions/67032084/…。但你的答案仍然是正确的答案。因为我不知道教程是认为读者遵循标准表。喜欢 id 应该 id。
    猜你喜欢
    • 2020-08-22
    • 2020-10-30
    • 2016-01-26
    • 1970-01-01
    • 2022-08-24
    • 2020-06-26
    • 1970-01-01
    • 2015-02-22
    • 2016-04-09
    相关资源
    最近更新 更多