如何做到拥有许多和属于许多在同一模型中?



我有2个模型,Employee & FieldReport。我需要根据以下条件创建关系:

现场报告由具有绝对性质的员工拥有 (必须显示所有者的数据且不能编辑(,其中 报告字段还有一个标记,用于标记员工是谁 报告字段。

员工本人有许多现场报告。

现在,我已经建立了一段关系,就像这样:

  1. 员工有许多现场报告。
  2. 员工属于许多现场报告。
  3. 现场报告属于员工。
  4. 现场报告属于许多员工。

然后我遇到了一个问题,PHP 不允许使用相同的方法名称(在员工模型中(。 例:

  • 有许多具有方法名称字段的报告 ((
  • 属于很多也有方法名字段的报告((

而如果我定义函数名称 custom,我无法获取填充第一个数据透视列的值并生成如下所示的错误:

SQLSTATE [23000]:完整性约束冲突:19 不为 NULL 约束失败:field_report_participant.field_report_id (SQL: 插入"field_report_participant"("id"、"participant_id"(值 (1, 2((

有什么解决办法吗?这是我的脚本的样子:

Employee.php


/**
* Each employee has many fieldReports.
*
* @return IlluminateDatabaseEloquentRelationshipHasMany
*/
public function fieldReports()
{
return $this->hasMany(FieldReport::class);
}
/**
* Each employee belongs to many fieldReports.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsToMany
*/
public function fieldReports()
{
return $this->belongsToMany(FieldReport::class);
}

FieldReportController.php

/**
* Store a newly created resource in storage.
*
* @param  AppHttpRequestsRequestFieldReport  $request
* @return IlluminateHttpResponse
*/
public function store(RequestFieldReport $request)
{
$fieldReport = $this->data($request, $this->storeImages($request));
$fieldReport->participants()->sync(
$request->participants
);
return response()->json([
'created' => true,
'data' => $fieldReport,
], 201);
}

FieldReport.php

/**
* Each field report belongs to a company.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class);
}
/**
* Each field report belongs to a employee.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsTo
*/
public function employee()
{
return $this->belongsTo(Employee::class);
}
/**
* Each field report belongs to many participants.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsToMany
*/
public function participants()
{
return $this->belongsToMany(Employee::class, 'field_report_participant', 'participant_id', 'id');
}

create_field_reports_table.php

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateFieldReportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('field_reports', function (Blueprint $table) {
$table->id();
$table->bigInteger('company_id');
$table->bigInteger('employee_id');
$table->string('title', 100);
$table->text('chronology');
$table->json('images')->nullable();
$table->timestamp('closed_at')->nullable();
$table->string('closed_by', 100)->nullable();
$table->timestamp('opened_at')->nullable();
$table->string('opened_by', 100)->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('field_reports');
}
}

field_report_participant_table.php

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateFieldReportParticipantTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('field_report_participant', function (Blueprint $table) {
$table->id();
$table->bigInteger('field_report_id');
$table->bigInteger('participant_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('field_report_participant');
}
}

经过一个小时,我扯掉头发,尝试做一个后空翻,并在每个不同的论坛上提问,终于我得到了答案。不幸的是,他在这个论坛上没有帐户,无法给出这个问题的答案。

问题是我在participants方法上放了一个错误的键,导致field_report_id放在错误的地方,在这种情况下;id.这是通过这样做来解决的:

/**
* Each field report belongs to many participants.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsToMany
*/
public function participants()
{
return $this->belongsToMany(Employee::class, 'field_report_participant', 'field_report_id', 'participant_id');
}

然后,在Employee类上,我可以创建完全不同的方法并将其与数据透视表链接。喜欢这个:

/**
* Each employee belongs to many assignedFieldReports.
*
* @return IlluminateDatabaseEloquentRelationshipBelongsToMany
*/
public function assignedFieldReports()
{
return $this->belongsToMany(FieldReport::class, 'field_report_participant', 'participant_id', 'field_report_id');
}

希望它可以帮助将来面临完全相同问题的人。

最新更新