Jan
17
2 days ago
Activity icon
issue

staudenmeir issue comment staudenmeir/eloquent-has-many-deep

staudenmeir
staudenmeir

Use hasManyDeepFromRelations on the same Model

Hello, i'm having trouble creating a new relationship that uses hasManyDeepFromRelations.

Error SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users'

I've tried to use HasTableAlias on User Model and Blog Model but it didn't work as expected

Expected result

Merge 3 relationships (user,creator,commenters) into one (participants). What i'm doing wrong here?

Blog Model

public function user(): BelongsTo
{
  return $this->belongsTo(User::class);
}

public function creator(): BelongsTo
{
    return $this->belongsTo(User::class);
}

public function commenters(): BelongsToMany
{
    return $this->belongsToMany(User::class, BlogComment::class)->distinct();
}

/**
 * Get participants.
 *
 * @todo: We should get the users who comments + creator + user
 * 
 * @return HasManyDeep
 */
public function participants(): HasManyDeep
{
    return $this->hasManyDeepFromRelations($this->commenters(), $this->user(), $this->creator());
}
staudenmeir
staudenmeir

This is actually a misunderstanding of the package's purpose: It allows you to concatenate/chain relationships (A -> B -> C -> D) but not to merge them – that's a different (and more complicated) topic. I also created a package for this: https://github.com/staudenmeir/laravel-merged-relations

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeView(
    'participants',
    [(new Blog)-> commenters(), (new Blog)-> user(), (new Blog)-> creator()]
);

class Blog extends Model
{
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function participants()
    {
        return $this->mergedRelationWithModel(User::class, 'participants');
    }
}
Activity icon
issue

mfmarques issue staudenmeir/eloquent-has-many-deep

mfmarques
mfmarques

Use hasManyDeepFromRelations on the same Model

Hello, i'm having trouble creating a new relationship that uses hasManyDeepFromRelations.

Error SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users'

I've tried to use HasTableAlias on User Model and Blog Model but it didn't work as expected

Expected result

Merge 3 relationships (user,creator,commenters) into one (participants). What i'm doing wrong here?

Blog Model

public function user(): BelongsTo
{
  return $this->belongsTo(User::class);
}

public function creator(): BelongsTo
{
    return $this->belongsTo(User::class);
}

public function commenters(): BelongsToMany
{
    return $this->belongsToMany(User::class, BlogComment::class)->distinct();
}

/**
 * Get participants.
 *
 * @todo: We should get the users who comments + creator + user
 * 
 * @return HasManyDeep
 */
public function participants(): HasManyDeep
{
    return $this->hasManyDeepFromRelations($this->commenters(), $this->user(), $this->creator());
}
Activity icon
issue

staudenmeir issue comment staudenmeir/eloquent-has-many-deep

staudenmeir
staudenmeir

hasManyDeepFromRelations does not work as expected

Record::resolveRelationUsing('expenses', function($model){
    return $model->hasManyDeepFromRelations(
        $model->hasMany(Record::class, 'data->project'), //get expenses
        $model->setAlias('project_steps')->hasMany(Record::class, 'data->project_step'), // get steps
    );
});

The relationship is in the same model;

sql result:

SELECT
    `records`.*,
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) AS `laravel_through_key`
FROM
    `records`
    INNER JOIN `records` AS `expenses` ON `expenses`.`id` = json_unquote(
        json_extract(`records`.`DATA`, '$."project_step"')
    )
WHERE
    and json_unquote(json_extract(`records`.`DATA`, '$."project"')) in (1)

Note that the last condition (where) verifies the root of the query for the relation Instead of validating the figure between the project_steps

I would only expect this change:

SELECT
    `expenses`.*,
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) AS `laravel_through_key`
FROM
    `records`
    INNER JOIN `records` AS `expenses` ON `records`.`id` = json_unquote(
        json_extract(`expenses`.`DATA`, '$."project_step"')
    )
WHERE
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) in (1)
staudenmeir
staudenmeir

Please share the Record model and the table's migration.

Activity icon
issue

staudenmeir issue comment staudenmeir/eloquent-has-many-deep

staudenmeir
staudenmeir

hasManyDeepFromRelations does not work as expected

Record::resolveRelationUsing('expenses', function($model){
    return $model->hasManyDeepFromRelations(
        $model->hasMany(Record::class, 'data->project'), //get expenses
        $model->setAlias('project_steps')->hasMany(Record::class, 'data->project_step'), // get steps
    );
});

The relationship is in the same model;

sql result:

SELECT
    `records`.*,
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) AS `laravel_through_key`
FROM
    `records`
    INNER JOIN `records` AS `expenses` ON `expenses`.`id` = json_unquote(
        json_extract(`records`.`DATA`, '$."project_step"')
    )
WHERE
    and json_unquote(json_extract(`records`.`DATA`, '$."project"')) in (1)

Note that the last condition (where) verifies the root of the query for the relation Instead of validating the figure between the project_steps

I would only expect this change:

SELECT
    `expenses`.*,
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) AS `laravel_through_key`
FROM
    `records`
    INNER JOIN `records` AS `expenses` ON `records`.`id` = json_unquote(
        json_extract(`expenses`.`DATA`, '$."project_step"')
    )
WHERE
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) in (1)
staudenmeir
staudenmeir
Activity icon
issue

sbc640964 issue staudenmeir/eloquent-has-many-deep

sbc640964
sbc640964

hasManyDeepFromRelations does not work as expected

Record::resolveRelationUsing('expenses', function($model){
    return $model->hasManyDeepFromRelations(
        $model->setAlias('project_steps')->hasMany(Record::class, 'data->project', 'id'),
        $model->hasMany(Record::class, 'data->project_step', 'id'),
    );
});

The relationship is in the same model;

sql result:

SELECT
    `records`.*,
    json_unquote(json_extract(`records`.`DATA`, '$."project"')) AS `laravel_through_key`
FROM
    `records`
    INNER JOIN `records` AS `project_steps` ON `project_steps`.`id` = json_unquote(
        json_extract(`records`.`DATA`, '$."project_step"')
    )
WHERE
    json_unquote(
        json_extract(`records`.`DATA`, '$."project"')
    ) in (1)

Note that the last condition (where) verifies the root of the query for the relation Instead of validating the figure between the project_steps

I would only expect this change:

WHERE
    json_unquote(
        json_extract(`project_steps`.`DATA`, '$."project"')
    ) in (1)
Previous