xx19941215
V2EX  ›  PHP

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

  •  
  •   xx19941215 · Mar 24, 2017 · 2774 views
    This topic created in 3359 days ago, the information mentioned may be changed or developed.

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

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

    相关表结构

    • products

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

      • id (主键,递增)
      • tag_id
      • taggable_type
      • taggable_id
    2 replies    2017-03-24 14:36:16 +08:00
    run2
        1
    run2  
       Mar 24, 2017
    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
       Mar 24, 2017
    @sobigfish 好像不太对啊,这里是第几个值来设置父表的键?我试了$foreignKey 不太对啊啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3526 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    ♥ Do have faith in what you're doing.