【问题标题】:Eloquent relationship issue : belongsTo雄辩的关系问题:belongsTo
【发布时间】:2020-07-03 03:30:55
【问题描述】:

我是 Laravel 的新手。

我有这些模型:

  • 模态,
  • 报告组,
  • 报告,
  • 用户

基本上,我正在尝试建立一些关系:

- 一个 ReportsGroup 属于一个 Modality,

- 一个 Modality 有许多 ReportsGroup,

- 一个 ReportsGroup 有许多报告,

- 报告属于 ReportsGroup,

- 一份报告有一个用户,

- 一个用户有很多报告。

(粗体线是我无法达到的关系......)

型号:

Modality.php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Modality extends Model
    {
        public function reportGroups()
        {
          $this->hasMany(ReportsGroup::class);
        }
    }

ReportsGroup.php:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReportsGroup extends Model
{
  public function modality()
  {
    return $this->belongsTo(Modality::class);
  }
  public function reports()
  {
    return $this->hasMany(Report::class);
  }
}

报告.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Report extends Model
{
    public function author()
    {
      return $this->belongsTo(User::class, 'author_id');
    }
    public function reportsGroup()
    {
      return $this->belongsTo(ReportsGroup::class, 'reportsGroup_id');
    }
}

迁移:

表格形式:

public function up()
    {
        Schema::create('modalities', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('is_free')->default(true);
            $table->timestamps();
        });
    }

表格报告组:

public function up()
{
  Schema::create('reports_groups', function (Blueprint $table) {
      $table->id();
      $table->unsignedBigInteger('modality_id');
      $table->string('title');
      $table->text('description')->nullable();
      $table->timestamps();
  });
}

表格报告

  public function up()
    {
        Schema::create('reports', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('reportsGroup_id');
            $table->string('title');
            $table->text('indication')->nullable();
            $table->text('technique')->nullable();
            $table->text('conclusion')->nullable();
            $table->text('recommandations')->nullable();
            $table->unsignedBigInteger('author_id');
            $table->boolean('is_visible')->defaut(true);
            $table->boolean('is_free')->default(true);
            $table->timestamps();

            $table->index('reportsGroup_id');
            $table->index('author_id');
        });
    }

表用户:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

正如我所写,似乎唯一有效的关系是 ReportsGrouop 属于 Modality...

我被这个问题困扰了好几天......

请帮忙! 谢谢,

尼古拉斯

【问题讨论】:

  • 您能否发布您的查询以获得适当的帮助?
  • 现在我正在使用 Tinker 进行测试!所以我只是想用 CLI 检查一下。
  • $table->id(); 你是怎么做到的?如果你迁移,你必须得到一个错误,比如错误的方法异常 id。
  • 不,我迁移时没有任何问题:/
  • 你使用的是哪个版本的 laravel?这必须给你错误。但是,如果不是,它会使 id 成为表的 pk 吗?

标签: laravel eloquent


【解决方案1】:

添加参考 ID,如

$table->unsignedBigInteger('modality_id');

是不够的。 您还应该添加一个外键:

$table->foreign('modality_id')->references('id')->on('modalities');

https://laravel.com/docs/6.x/migrations#foreign-key-constraints

【讨论】:

  • 谢谢!但是模式 报告组链接在没有 !与报表模型的关系不起作用...
  • 嗯,好吧,也许外国不是必需的。无论如何仔细看看以下似乎不行:在Modality.php中,reportGroups函数reportGroups名称与其他名称不一致,应该是reportsGroups在ReportsGroup.php中的函数reports中,您需要将索引名称'reportsGroup_id'指定为好吧,否则它会寻找reports_group_id
  • 谢谢朱利安!如何指定索引名称?
  • 与您在 Reports.php 中所做的完全一样:public function reports() { return $this->hasMany(Report::class, 'reportsGroup_id'); }
  • 这(加上纠正一些代码错误)修复了一切!谢谢大家!
猜你喜欢
  • 2019-03-17
  • 2014-10-18
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2015-11-01
  • 1970-01-01
相关资源
最近更新 更多