#hasOneThrough Key Conventions

6 messages · Page 1 of 1 (latest)

worthy cosmos
#

Hello, i want the newsletter to have relationship to hero and header via info
so I can reference $newsletter->hero and $newsletter->header

I am having trouble creating hasOneThrough, please help with conventions.
Note: Newsletter hasOne info. So, Newsletter will have One hero and One Header through Info

Schema::create('infos', function (Blueprint $table) {
            $table->id();
            $table->foreignUuid('newsletter_id')
                ->constrained()
                ->cascadeOnDelete();
            $table->foreignIdFor(Hero::class)->nullable();
            $table->foreignIdFor(Header::class)->nullable();
            $table->timestamps();
        });

        Schema::create('newsletters', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('heroes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('path');
            $table->string('link', 2048);
            $table->timestamps();
        });

        Schema::create('headers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('path');
            $table->string('link', 2048);
            $table->timestamps();
        });
#

From the docs.

$this->hasOneThrough(Hero::class, Info::class)

nets me SQLSTATE[42S22]: Column not found: 1054 Unknown column 'heroes.info_id' in 'on clause' (Connection: mysql, SQL: select `heroes`.*, `infos`.`newsletter_id` as `laravel_through_key` from `heroes` inner join `infos` on `infos`.`id` = `heroes`.`info_id` where `infos`.`newsletter_id` = 991bc70d-db9e-4447-99a9-66665cd0c2fb limit 1).

sinful sundial
#

@worthy cosmos this bit me earlier. Try this

$this->hasOneThrough(Hero::class, Info::class, 'id', 'id', 'info_id', 'hero_id')
worthy cosmos
# sinful sundial <@673114917783601183> this bit me earlier. Try this ```php $this->hasOneThrough...

I get null out of it

> $n = \App\Models\Newsletter::first();

= App\Models\Newsletter {#7761

    id: "991bc70d-db9e-4447-99a9-66665cd0c2fb",

    name: "SJC",

    created_at: "2023-05-07 07:50:27",

    updated_at: "2023-05-07 07:50:27",

  }



> $n->hero

= null



> $n->info

= App\Models\Info {#7767

    id: 1,

    newsletter_id: "991bc70d-db9e-4447-99a9-66665cd0c2fb",

    hero_id: 8,

    header_id: 2,

    created_at: "2023-05-08 11:32:52",

    updated_at: "2023-05-08 12:09:51",

  }



> $n->info->hero

= App\Models\Hero {#7771

    id: 8,

    name: "Fake hero",

    path: "heroes/abAQye3mMJEoe7Y8LtUFQpddxSfcU3-metaY2xpbmljX25ld3NfbG9nby5qcGc=-.jpg",

    link: "fake.com",

    created_at: "2023-05-07 22:23:20",

    updated_at: "2023-05-07 22:23:20",

    +cdn: "https://khaos.fra1.cdn.digitaloce....",

  }

sinful sundial
#

what's the relationship between info and hero?

#

belongsTo?