Laravel多对多提供了意想不到的价值

时间:2019-03-13 17:13:57

标签: laravel eloquent many-to-many eloquent--relationship

在Laravel项目中,我有一个想法,那就是在称为Kit的模型与其他两个模型Group和Item之间建立两个简单的多对多口才关系。

数据库中的所有内容似乎都是正确的,但是当我尝试访问这些关系时,我得到了一些非常意外的结果。

这是数据透视表的外观(我将MySQL表导出为PHP数组,以便在此处可以更轻松地查看它们)。

$group_kit = array(
  array('group_no' => '401','kit_id' => '1'),
  array('group_no' => '403','kit_id' => '1'),
  array('group_no' => '404','kit_id' => '1'),
  array('group_no' => '405','kit_id' => '1'),
  array('group_no' => '401','kit_id' => '2'),
  array('group_no' => '403','kit_id' => '2'),
  array('group_no' => '404','kit_id' => '2'),
  array('group_no' => '405','kit_id' => '2')
);

$item_kit = array(
  array('item_no' => '3','kit_id' => '1'),
  array('item_no' => '3','kit_id' => '2')
);

在我的PHPMyAdmin数据库中,所有关系都存在。例如,如果我单击3数据透视表的item_no列中的item_kit,则会带我到Item表中items的实例其item_no为3。

但是,在我的刀片服务器模板中,此代码没有显示我期望看到的内容:

<p>Kit ID: {{ $kit->id }}</p>

<p>Kit Groups Count: {{ $kit->groups->count() }}</p>

<p>Kit Items Count: {{ $kit->items->count() }}</p>

<p>Kit Creator: {{ $kit->user->first_name }}</p>

<ul>
    @foreach($kit->groups as $group)

        <li>Item: {{ $group->group_no }}</li>

    @endforeach
</ul>

<ul>
    @foreach($kit->items as $item)

        <li>Item: {{ $item->item_no }}</li>

    @endforeach
</ul>

期待,例如:

套件ID:2
套件组数:4
套件项目数:1
套件创建者:盖伦

  • 401
  • 403
  • 404
  • 405

  • 3


但是我实际上得到的是这个

套件ID:2
套件组数:0
套件项目数:1
套件创建者:盖伦

  • 2

这是Kit模型-相关的功能是groups()和items():

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Kit extends Model
{

    protected $guarded = [];

    public function organization() 
    {
    return $this->belongsTo(Organization::class); 
    }

    public function groups()
    {
        return $this->belongsToMany(Group::class, 'group_kit', 'kit_id', 'group_no');
    }

    public function items()
    {
        return $this->belongsToMany(Item::class, 'item_kit', 'kit_id', 'item_no');
    }

    public function notes()
    {
        return $this->hasMany(Note::class);
    }

    public function event()
    {
        return $this->belongsTo(Event::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}

这是Group模型-相关的功能是kits():

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    protected $guarded = [];
    public function getRouteKeyName()
    {
    return 'group_no';
    }
    public function items()
    {
        return $this->hasMany(Item::class, 'group_no', 'group_no');
    }
    public function parent()
    {
        return $this->belongsTo(Group::class, 'parent_group', 'group_no');
    }
    public function children()
    {
        return $this->hasMany(Group::class, 'parent_group', 'group_no');
    }

    public function kits()
    {
        return $this->belongsToMany(Kit::class, 'group_kit', 'group_no', 'kit_id');
    }

    public function notes()
    {
        return $this->hasMany(Note::class, 'group_no', 'group_no');
    }

}

这是Item模型-相关的功能是kits():

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{

    protected $guarded = [];

    public function getRouteKeyName()
    {
    return 'item_no';
    }

    public function group()
    {
        return $this->belongsTo(Group::class, 'group_no', 'group_no');
    }

    public function kits()
    {
        return $this->belongsToMany(Kit::class, 'item_kit', 'item_no', 'kit_id');
    }

    public function notes()
    {
        return $this->hasMany(Note::class, 'item_no', 'item_no');
    }
}

这是group_kit表的架构:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGroupKitTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('group_kit', function (Blueprint $table) {
            $table->unsignedInteger('group_no');
            $table->unsignedInteger('kit_id');

            $table->foreign('group_no')->references('group_no')->on('groups')->onDelete('cascade');
            $table->foreign('kit_id')->references('id')->on('kits')->onDelete('cascade');

            $table->primary(['group_no', 'kit_id']);

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('group_kit');
    }
}

这是item_kit表的架构:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateItemKitTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('item_kit', function (Blueprint $table) {
            $table->unsignedInteger('item_no');
            $table->unsignedInteger('kit_id');

            $table->foreign('item_no')->references('item_no')->on('items')->onDelete('cascade');
            $table->foreign('kit_id')->references('id')->on('kits')->onDelete('cascade');

            $table->primary(['item_no', 'kit_id']);

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('item_kit');
    }
}

group_no表上的groups列和item_no表上的items列都是unsignedInteger类型。

我想不出我要忽略的其他事情。也许别人看到了吗?

0 个答案:

没有答案