nicolas_geneva
nicolas_geneva

Reputation: 23

Eloquent relationship issue : belongsTo

I'm new with Laravel.

I have these Models :

Basically, I'm trying to build some relationship :

- A ReportsGroup belongs to a Modality,

- A Modality has many ReportsGroups,

- A ReportsGroup has many Reports,

- A Report belongs to a ReportsGroup,

- A Report has one User,

- A User has many Reports.

(the lines in bold are the relationship I cannot reach...)

MODEL :

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);
  }
}

Report.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');
    }
}

MIGRATIONS :

Table modalities :

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();
        });
    }

Table reportsGroups :

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();
  });
}

Table reports

  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');
        });
    }

Table users :

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();
    });
}

As I wrote, the only relationship that seems to work is the fact that a ReportsGrouop belongs to a Modality...

It's been days I am stuck with this issue...

Please help! Thanks,

Nicolas

Upvotes: 1

Views: 312

Answers (1)

Julien
Julien

Reputation: 76

Adding a reference ID like

$table->unsignedBigInteger('modality_id');

is not sufficient. You should also add a foreign key:

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

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

Upvotes: 2

Related Questions