Php Laravel通过具有价值的中间表关联2个模型

Php Laravel通过具有价值的中间表关联2个模型,php,laravel,eloquent,Php,Laravel,Eloquent,我有以下模式 创建表“属性”( `id`bigint(20)无符号非空自动增量, `name`varchar(191)COLLATE utf8mb4\u unicode\u ci非空, `在“timestamp NULL DEFAULT NULL”处创建了_, `在`时间戳为空默认为空'处更新了_, `已在“时间戳为空默认为空”处删除\u, 主键(`id`) )ENGINE=InnoDB AUTO_INCREMENT=5默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_

我有以下模式

创建表“属性”(
`id`bigint(20)无符号非空自动增量,
`name`varchar(191)COLLATE utf8mb4\u unicode\u ci非空,
`在“timestamp NULL DEFAULT NULL”处创建了_,
`在`时间戳为空默认为空'处更新了_,
`已在“时间戳为空默认为空”处删除\u,
主键(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=5默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_ci;
创建表“记录”(
`id`bigint(20)无符号非空自动增量,
`事件_school_id`bigint(20)未签名非空,
`运动员id`bigint(20)未签名非空,
`年份`int(10)无符号不为空,
`置`int(10)为无符号非空,
`在“timestamp NULL DEFAULT NULL”处创建了_,
`在`时间戳为空默认为空'处更新了_,
`已在“时间戳为空默认为空”处删除\u,
主键(`id`),
键“records\u event\u school\u id\u foreign”(“event\u school\u id”),
键“records\u Athletor\u id\u foreign”(“Athletor\u id”),
约束'records\u Athletator\u id\u foreign'外键('Athletator\u id')引用'Athleters'('id'),
约束`records\u event\u school\u id\u foreign`外键(`event\u school\u id`)引用`event\u school`(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=4默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_ci;
创建表“属性\记录”(
`id`bigint(20)无符号非空自动增量,
`属性_id`bigint(20)无符号非空,
`记录_id`bigint(20)无符号非空,
`值`十进制(8,2)不为空,
`在“timestamp NULL DEFAULT NULL”处创建了_,
`在`时间戳为空默认为空'处更新了_,
`已在“时间戳为空默认为空”处删除\u,
主键(`id`),
键`attribute\u record\u attribute\u id\u foreign`(`attribute\u id`),
键`attribute\u record\u id\u foreign`(`record\u id`),
约束`attribute\u record\u attribute\u id\u foreign`外键(`attribute\u id`)引用`attributes`(`id`),
约束`attribute\u record\u id\u foreign`外键(`record\u id`)引用`records`(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=7默认字符集=utf8mb4 COLLATE=utf8mb4_unicode_ci;
如何在laravel中正确设置模型?对于属性\记录表,属性和记录之间的关系中存在一个值。我想知道我是否需要属性记录表的模型

我希望能够做一些事情,我可以有一个$record获取属性和它们的值

foreach($record->attributes as $attr)
{
   echo $attr->value;
} 
这是我到目前为止所拥有的

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Attribute extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];

}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Record extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];
}

我想知道我是否需要属性记录表的模型

不,这是一个透视表,当您正确设置关系时,Laravel将透明地使用它。我假设这是一个(记录可以有许多属性,许多记录可以有相同的属性),因此定义您的关系,其余的由Laravel完成:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Attribute extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];
    public function records()
    {
        return $this->belongsToMany('\\App\\Record')
            ->withPivot('value')
            ->withTimestamps();
    }
}


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Record extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];
    public function attributes()
    {
        return $this->belongsToMany('\\App\\Attributes')
            ->withPivot('value')
            ->withTimestamps();
    }
}
我想知道我是否需要属性记录表的模型

不,这是一个透视表,当您正确设置关系时,Laravel将透明地使用它。我假设这是一个(记录可以有许多属性,许多记录可以有相同的属性),因此定义您的关系,其余的由Laravel完成:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Attribute extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];
    public function records()
    {
        return $this->belongsToMany('\\App\\Record')
            ->withPivot('value')
            ->withTimestamps();
    }
}


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Record extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];
    public function attributes()
    {
        return $this->belongsToMany('\\App\\Attributes')
            ->withPivot('value')
            ->withTimestamps();
    }
}