Laravel ManyToMany在同一型号上的关系

问题描述 投票:1回答:3

我正在尝试在我的Tag模型上建立双向ManyToMany关系,但我遇到了这个“问题”。

我的模型看起来像这样:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    protected $table = 'tags';
    public $timestamps = false;

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'tag_tag', 'tag_one_id', 'tag_two_id');
    }

}

所以现在让我说我的标签表中有Tag1和Tag2,然后我将Tag2与Tag1联系起来。现在我的数据透视表将如下所示:

+----+------------+------------+
| id | tag_one_id | tag_two_id |
+----+------------+------------+
| 1  | 1          | 2          |
+----+------------+------------+

当我尝试这段代码时:

$tag = Tag::find(1);
$tag->tags()->get();

我得到了Tag2实例,这是正确的。

但是当我尝试运行此代码时:

$tag = Tag::find(2);
$tag->tags()->get();

我想收到Tag1实例,但我没有。

是否可以使用Laravel默认Eloquent在模型上只使用一种方法来完成它?

php mysql laravel eloquent relationship
3个回答
2
投票

为什么它不起作用?

它不起作用,因为在Tag模型中添加的关系被定义为单向工作。但不是相反的方式。

如果我们可以定义两个名为tags()的方法,它将起作用,如下所示:

public function tags()
{
  return $this->belongsToMany(Tag::class, 'tag_tag', 'tag_one_id', 'tag_two_id');
}

//and

public function tags()
{
  return $this->belongsToMany(Tag::class, 'tag_tag', 'tag_two_id', 'tag_one_id');
}

不幸的是,这是不可能的。

那么,什么是可能的解决方案

一种可能的解决方案是,不要触及这种关系。相反,如果你能以某种方式设法为这些关系插入两个关系,那么它将起作用。例如:

+----+------------+------------+
| id | tag_one_id | tag_two_id |
+----+------------+------------+
| 1  | 1          | 2          |
+----+------------+------------+
| 1  | 2          | 1          |
+----+------------+------------+

这是我现在想到的解决方案。可能还有更好的解决方案。


0
投票

这是可能的。您必须将参数传递给tags()方法,并使用该参数修改关系上的主/外键字段。尽管如此,这可能是一个彻头彻尾的痛苦,而且制作第二种关系方法几乎肯定不会让人头痛。它最终看起来像这样:

public function tags($tag1 = 'tag_one_id', $tag2 = 'tag_two_id')
{
    return $this->belongsToMany(Tag::class, 'tag_tag', $tag1, $tag2);
}

然后你需要在需要Tag::find(2)->tags('tag_two_id', 'tag_one_id')时修改这些值

这可以如下所述加载:https://laracasts.com/discuss/channels/general-discussion/eager-load-with-parameters

您的用例可能比您的帖子建议的要复杂得多,这可能会使这更合理。因为它是我会考虑一些其他选项。


0
投票

我找到了解决方案,我就这样解决了。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    /**
     * @inheritdoc
     *
     * @var string
     */
    protected $table = 'tags';

    /**
     * @inheritdoc
     *
     * @var bool
     */
    public $timestamps = false;

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    /**
     * Every tag can contain many related tags (TagOne has many TagTwo).
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    protected function tagsOneTwo()
    {
        return $this->belongsToMany(Tag::class, 'tag_tag', 'tag_one_id', 'tag_two_id');
    }

    /**
     * Every tag can contain many related tags (TagTwo has many TagOne).
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    protected function tagsTwoOne()
    {
        return $this->belongsToMany(Tag::class, 'tag_tag', 'tag_two_id', 'tag_one_id');
    }

    /**
     * This method returns a collection with all the tags related with this tag.
     * It is not a real relation, but emulates it.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function tags()
    {
        return $this->tagsOneTwo()->get()->merge($this->tagsTwoOne()->get())->unique('id');
    }

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /**
     * Function to relate two tags together.
     *
     * @param Tag $tag
     * @return void;
     */
    public function attach(Tag $tag)
    {
        if ($this->tags()->contains('id', $tag->getKey())) {
            return;
        }

        $this->tagsOneTwo()->attach($tag->getKey());
    }

    /**
     * Function to un-relate two tags.
     *
     * @param Tag $tag
     * @return void;
     */
    public function detach(Tag $tag)
    {
        if ($this->tags()->contains('id', $tag->getKey())) {
            // Detach the relationship in both ways.
            $this->tagsOneTwo()->detach($tag->getKey());
            $this->tagsTwoOne()->detach($tag->getKey());
        }
    }

    /*
    |--------------------------------------------------------------------------
    | ACCESORS
    |--------------------------------------------------------------------------
    */

    /**
     * Let access the related tags like if it was preloaded ($tag->tags).
     *
     * @return mixed
     */
    public function getTagsAttribute()
    {
        if (! array_key_exists('tags', $this->relations)) {
            $this->setRelation('tags', $this->tags());
        };

        return $this->getRelation('tags');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.