使用 Laravel 访问多对多关系

Accessing Many to Many Relationships with Laravel

所以我遇到的问题是访问和使用来自多对多关系的数据,我在 October cms 中使用 Laravel。

我有 Owners 和 ManyToMany 对象 pivot table 使用 owner_id and manytomany_id。我可以使用下面的代码访问 OneToMany relationship 就好了,但是尝试访问 ManyToMany objects 我无法分配它们,这是我正在使用的代码,

    foreach ($this->owners as $owner) {
        $owner['onetomany'] = $owner->onetomany()->take($this->maxJobs)->get();
        $owner['manytomany'] = $owner->manytomany()->take($this->maxTeam)->get();    
    }

我的关系都是这样定义的

public $belongsToMany = [
    'manytomany' => [
        'Kai\Relations\Models\ManyToMany', 
        'table' => 'kai_relations_owner_many_to_many'
    ]
];
public $belongsToMany = [
    'owner' => [
        'Kai\Relations\Models\Owner', 
        'table' => 'kai_relations_owner_many_to_many'
    ]
];

如果我定义一个页面变量,例如 $this->something,它不允许我分配 ManyToMany 查询的结果,但如果我将结果分配给 $something,我可以记录结果并看到返回的数据,为什么访问不了?

我没有预先加载这些结果的原因是需要限制项目的数量,经过大量研究后我发现预先加载无法做到这一点。

这是我收到的错误:

SQLSTATE[HY000]: General error: 2031 (SQL: select * from `kai_relations_many_to_manies` where `id` in (?))

如果有人能阐明这一点,那将会很有帮助!

在文档中:https://laravel.com/docs/5.3/eloquent-relationships#many-to-many 说关系有一个枢轴 table 并且在这个 table 中有参考 table 的列来建立多对多关系。

例如迁移文件:

Schema::create('category_product', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('category_id');
    $table->integer('product_id');
});;

在产品型号中:

public function extraCategories()
{
    return $this->belongsToMany(Category::class);
}

在类别模型中:

public function extraProducts()
{
    return $this->belongsToMany('App\Product');
}

其他模型示例:

public function associacion()
{
    return $this->belongsToMany('App\Product', 'product_related', 'product2_id', 'product_id');
}

在这种情况下,有一个特定的 table,第一列和第二列,Eloquent 不会自动与枢轴 table 和其他模型建立连接。

当您使用 tinker 获取数据时,您可以看到:

App\Product {#892
     id: "4555",
     name: "PRODUCT NAME",
     extraCategories: Illuminate\Database\Eloquent\Collection {#912
       all: [
         App\Category {#907
           id: "245",
           description: "YYYYY",
           pivot: Illuminate\Database\Eloquent\Relations\Pivot {#902
             product_id: "4555",
             category_id: "245",
           },
         },
         App\Category {#908
           id: "135",
           description: "XXXXX",
           pivot: Illuminate\Database\Eloquent\Relations\Pivot {#901
             product_id: "4555",
             category_id: "135",
           },
         },
       ],
     },
   }

在这种情况下,返回了集合数组上多对多的 extraCategories 关系。

希望这能解决您的问题。

foreach ($this->owners as $owner) {
        $owner['onetomany'] = $owner->onetomany()->take($this->maxJobs)->get();
        $owner['manytomany'] = $owner->manytomany()->take($this->maxTeam)->get();    
    }

我认为这不起作用,因为您已经定义了与名称 manytomany 的关系,因此它将尝试覆盖您的 manytomany 使用您尝试使用 $owner->manytomany()->take($this->maxTeam)->get();

检索的数据

现在您可以将名称从 $owner['manytomany'] 更改为与您的关系名称不同的名称,即 $owner['ownerManyToMany'] 然后您可能可以获得您的数据。

试试这个

foreach ($this->owners as $owner) {
        $owner['onetomany'] = $owner->onetomany()->take($this->maxJobs)->get();
        $owner['ownerManyToMany'] = $owner->manytomany()->take($this->maxTeam)->get();    
    }