在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
套件创建者:盖伦
405
3
但是我实际上得到的是这个
套件ID:2
套件组数:0
套件项目数:1
套件创建者:盖伦
这是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类型。
我想不出我要忽略的其他事情。也许别人看到了吗?