V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xx19941215
V2EX  ›  PHP

请问使用 laravel 的多对多的多态关联父表主键必须是叫做'id'吗?

  •  
  •   xx19941215 · 2017-03-24 13:47:38 +08:00 · 2323 次点击
    这是一个创建于 2583 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我叫做 eid 好像不好使啊。 是用在 product 和 tag 这两个实体之间。 这是定义在 product 模型上的

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

    相关表结构

    • products

      • eid (主键)
      • ...
    • tagables

      • id (主键,递增)
      • tag_id
      • taggable_type
      • taggable_id
    2 条回复    2017-03-24 14:36:16 +08:00
    sobigfish
        1
    sobigfish  
       2017-03-24 14:15:10 +08:00
    https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_morphToMany
    MorphToMany morphToMany(string $related, string $name, string $table = null, string $foreignKey = null, string $relatedKey = null, bool $inverse = false)
    xx19941215
        2
    xx19941215  
    OP
       2017-03-24 14:36:16 +08:00
    @sobigfish 好像不太对啊,这里是第几个值来设置父表的键?我试了$foreignKey 不太对啊啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2653 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:58 · PVG 09:58 · LAX 18:58 · JFK 21:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.