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

我有以下架构


                                                                                                                                                                                                                                                                                                                                                                     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;


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


慕姐4208626
浏览 162回答 2
2回答

慕姐8265434

我想知道是否需要attribute_record 表的模型。不,这是一个数据透视表,当你正确设置关系时,Laravel 会透明地使用它。我假设这是一个多对多的关系(记录可以有很多属性,很多记录可以有相同的属性)所以定义你的关系,剩下的由 Laravel 完成:<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;class Attribute extends Model{&nbsp; &nbsp; use SoftDeletes;&nbsp; &nbsp; protected $guarded = ['id'];&nbsp; &nbsp; public function records()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('\\App\\Record')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withPivot('value')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withTimestamps();&nbsp; &nbsp; }}<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;class Record extends Model{&nbsp; &nbsp; use SoftDeletes;&nbsp; &nbsp; protected $guarded = ['id'];&nbsp; &nbsp; public function attributes()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('\\App\\Attributes')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withPivot('value')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withTimestamps();&nbsp; &nbsp; }}现在在控制器中你可以这样做:$record = \App\Record::find($id);foreach ($record->attributes as $attribute) {&nbsp; &nbsp; // $attribute is an instance of \App\Attribute&nbsp; &nbsp; // to access the values in the pivot table, use the pivot attribute&nbsp; &nbsp; echo $attribute->pivot->value;}

萧十郎

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;class Record extends Model{&nbsp; &nbsp; use SoftDeletes;&nbsp; &nbsp; protected $guarded = ['id'];&nbsp; &nbsp; public function attributes()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany(Attribute::class, 'attribute_record')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withPivot('value');&nbsp; &nbsp; }}然后你可以访问这样的值:foreach($record->attributes as $attribute) {&nbsp; &nbsp; echo $attribute->pivot->value;}当您使用withPivot方法指定值时,您可以在 下访问它们$relatedInstance->pivot->yourColumn。枢轴值是从中间关系表中检索的(attribute_record在您的示例中)
打开App,查看更多内容
随时随地看视频慕课网APP