staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

hasManyDeep and BelongsTo #156

Closed jechazelle closed 2 years ago

jechazelle commented 2 years ago

Hi ! 👋

I use your package eloquent-has-many-deep : thank you so much for your job : it’s really awesome ! 🚀

Currently I have a question about your package, I have this tables :

CONTAINERS : id
CONTAINERS_SECTIONS_LINKER : container_id, section_id
SECTIONS : id
SECTIONS_BLOCKS_LINKER : section_id, blocks_id
BLOCKS : id, block_type_id
BLOCK_TYPES : id, name, code
FRAGMENTS : id

I use your package to get hasManyDeep sections -> hasMany -> blocks -> hasMany -> fragments with this on section model :

public function fragments()
{
    return $this->hasManyDeep(Fragment::class, ['sections_blocks_linker', Block::class, 'blocks_fragments_linker']);
}

It works !

But I would like to get the block type :

Container -> HasMany -> Section -> HasMany -> Block -> BelongsTo -> BlockType

I added this in the section model :

public function blockType()
{
    return $this->hasOneDeep(BlockType::class, ['sections_blocks_linker', Block::class, 'block_types']);
}

But it doesn’t work, do you have an idea please ?

My eloquent request :

$result = $this->container
    ->with(['sections' => function ($query) {
        $query->with(['blocks' => function ($query) {
            $query->with('blockType')
                ->with(['fragments' => function ($query) {
                $query->with('fragmentQuestions')
            }]);
        }]);
    }])
    ->first();

Thank you so much for your answer

staudenmeir commented 2 years ago

Do you have a block_type_id/type_id column in the blocks table?

jechazelle commented 2 years ago

Yes, I have a block_type_id column in the blocks table, I updated my code.

With this relation :

public function blockType()
    {
        return $this->hasOneDeep(BlockType::class, ['sections_blocks_linker', Block::class, 'block_types']);
    }

I have this error :

Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'block_types' (SQL: select `block_types`.*, `sections_blocks_linker`.`section_id` as `laravel_through_key` from `block_types` inner join `block_types` on `block_types`.`block_type_id` = `block_types`.`id` inner join `blocks` on `blocks`.`id` = `block_types`.`block_id` inner join `sections_blocks_linker` on `sections_blocks_linker`.`block_id` = `blocks`.`id` where `sections_blocks_linker`.`section_id` in (1))
staudenmeir commented 2 years ago

Use this relationship:

public function blockType()
{
    return $this->hasOneDeep(
        BlockType::class,
        ['sections_blocks_linker', Section::class, 'sections_blocks_linker', Block::class],
        [null, null, null, null, 'id'],
        [null, null, null, null, 'block_type_id']
    );
}
jechazelle commented 2 years ago

With this relationship (on section model), I have this error :

Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'sections_blocks_linker' (SQL: select `block_types`.*, `sections_blocks_linker`.`section_id` as `laravel_through_key` from `block_types` inner join `blocks` on `blocks`.`block_type_id` = `block_types`.`id` inner join `sections_blocks_linker` on `sections_blocks_linker`.`block_id` = `blocks`.`id` inner join `sections` on `sections`.`id` = `sections_blocks_linker`.`section_id` inner join `sections_blocks_linker` on `sections_blocks_linker`.`section_id` = `sections`.`id` where `sections_blocks_linker`.`section_id` in (1))
staudenmeir commented 2 years ago
public function blockType()
{
    return $this->hasOneDeep(
        BlockType::class,
        ['sections_blocks_linker', Block::class],
        [null, null, 'id'],
        [null, null, 'block_type_id']
    );
}
jechazelle commented 2 years ago

Whouu, perfect : Thank you so much @staudenmeir !!