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

时间:2019-09-13 18:05:24

标签: php laravel eloquent

我有以下模式

.onCommand()

如何在laravel中正确设置模型?对于attribute_record表,属性和记录之间的关系中存在一个值。我想知道是否需要为attribute_record表建立模型。

我希望能够做一些可以让$ record获取属性及其值的事情。

                                                                                                                                                                                                                                                                                                                                                                     CREATE TABLE `attributes` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `records` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `event_school_id` bigint(20) unsigned NOT NULL,
      `athlete_id` bigint(20) unsigned NOT NULL,
      `year` int(10) unsigned NOT NULL,
      `place` int(10) unsigned NOT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `records_event_school_id_foreign` (`event_school_id`),
      KEY `records_athlete_id_foreign` (`athlete_id`),
      CONSTRAINT `records_athlete_id_foreign` FOREIGN KEY (`athlete_id`) REFERENCES `athletes` (`id`),
      CONSTRAINT `records_event_school_id_foreign` FOREIGN KEY (`event_school_id`) REFERENCES `event_school` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `attribute_record` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `attribute_id` bigint(20) unsigned NOT NULL,
      `record_id` bigint(20) unsigned NOT NULL,
      `value` decimal(8,2) NOT NULL,
      `created_at` timestamp NULL DEFAULT NULL,
      `updated_at` timestamp NULL DEFAULT NULL,
      `deleted_at` timestamp NULL DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `attribute_record_attribute_id_foreign` (`attribute_id`),
      KEY `attribute_record_record_id_foreign` (`record_id`),
      CONSTRAINT `attribute_record_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`),
      CONSTRAINT `attribute_record_record_id_foreign` FOREIGN KEY (`record_id`) REFERENCES `records` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这是我到目前为止所拥有的。

foreach($record->attributes as $attr)
{
   echo $attr->value;
} 

2 个答案:

答案 0 :(得分:1)

  

我想知道是否需要为attribute_record表建立模型。

不,这是数据透视表,当您正确设置关系时,Laravel将透明地使用它。我假设这是一个many-to-many relationship(记录可以具有许多属性,并且许多记录可以具有相同属性),所以定义您的关系,其余的由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();
    }
}

现在可以在控制器中执行以下操作:

$record = \App\Record::find($id);
foreach ($record->attributes as $attribute) {
    // $attribute is an instance of \App\Attribute
    // to access the values in the pivot table, use the pivot attribute
    echo $attribute->pivot->value;
}

答案 1 :(得分:1)

<?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(Attribute::class, 'attribute_record')
            ->withPivot('value');
    }
}

然后您可以访问以下值:

foreach($record->attributes as $attribute) {
    echo $attribute->pivot->value;
}

使用withPivot方法指定值时,可以在$relatedInstance->pivot->yourColumn下访问它们。

枢轴值是从中间关系表(在您的示例中为attribute_record)中检索的