staudenmeir / eloquent-has-many-deep

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

id wrong set in relation #63

Closed coaradsupp closed 4 years ago

coaradsupp commented 4 years ago

hi, tnx for this package i use this format for better explaine:


//Office Model

/**
     * relation of office with reserve.
     * @return HasManyThrough
     */
    public function reserves() {
        return $this->hasManyThrough( Reserve::class , Worktime::class );
    }

    /**
     * relation of office with worktime.
     * @return HasMany
     */
    public function worktimes() {
        return $this->hasMany( Worktime::class );
    }
/////////////////////////////////////////////////////////////////////////////

//Worktime Model

/**
     * relation of worktime with office.
     * @return BelongsTo
     */
    public function office() {
        return $this->belongsTo( Office::class );
    }

    /**
     * relation of worktime with reserve.
     * @return HasMany
     */
    public function reserves() {
        return $this->hasMany( Reserve::class );
    }

/////////////////////////////////////////////////////////////////////////////

//Reserve Model

    /**
     * relation of reserve with office.
     * @return HasOneDeep
     */
    public function office() {
        return $this->hasOneDeepFromRelations( $this->worktime(), (new Worktime)->office());
    }

    /**
     * relation of worktime with office.
     * @return BelongsTo
     */
    public function worktime() {
        return $this->belongsTo( Worktime::class );
    }
/////////////////////////////////////////////////////////////////////////////

in this relation

$reserve = Reserve::find(43); dd($reserve->load('office'));

id of office wrong worktime_id set to office id

how solve this problem????? Capture

staudenmeir commented 4 years ago

What's the result of dd(Reserve::find(43)->getAttributes(), Reserve::find(43)->worktime->getAttributes());?

coaradsupp commented 4 years ago

thank you for your response

this result Capture1

staudenmeir commented 4 years ago

I'll look into it.

staudenmeir commented 4 years ago

What version of the package are you using (composer show)?

What's the result of this snippet?

\DB::enableQueryLog();
Reserve::find(43)->load('office');
dd(\DB::getQueryLog());
coaradsupp commented 4 years ago

this version : "staudenmeir/eloquent-has-many-deep": "^1.7",

10 instead 43

\DB::enableQueryLog();
        Reserve::find(10)->load('office');
        dd(\DB::getQueryLog());

return this result: Capture

staudenmeir commented 4 years ago

What does composer show show for staudenmeir/eloquent-has-many-deep?

What's your Laravel version?

coaradsupp commented 4 years ago
staudenmeir/eloquent-has-many-deep         v1.7     Laravel Eloquent HasManyThrough relationship with unlimited levels

laravel/framework                          v5.6.39  The Laravel Framework.
staudenmeir commented 4 years ago

This is/was a Laravel issue and has been fixed in Laravel 5.8.

If you can't upgrade, you can use one of my other packages: https://github.com/staudenmeir/belongs-to-through

class Reserve extends Model
{
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function office()
    {
        return $this->belongsToThrough(Office::class, Worktime::class);
    }
}
coaradsupp commented 4 years ago

Thanks for your good and complete answers

so use this package