繁体   English   中英

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

[英]Laravel Relate 2 models via intermediary table that has value

我有以下架构

                                                                                                                                                                                                                                                                                                                                                                     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;

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

我希望能够做一些可以让$ 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'];
}

我想知道是否需要attribute_record表的模型。

不,这是一个数据透视表,当您正确设置关系时,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();
    }
}

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

$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;
}
<?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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM