staudenmeir / eloquent-has-many-deep

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

Doesn't work on non existing models #146

Closed navaneeth-raman-bhaskar closed 2 years ago

navaneeth-raman-bhaskar commented 3 years ago

Price.php (model having pricingSchedule, currency relationships)

    public function pricingSchedule(): BelongsTo
    {
        return $this->belongsTo(PricingSchedule::class, 'pricing_schedule_id', 'id');
    }

    public function currency(): HasOneDeep
    {
        return $this->hasOneDeepFromRelations($this->pricingSchedule(),$this->pricingSchedule()->getRelated()->currency());
    }

PricingSchedule.php (model having currency relationship)

   public function currency(): BelongsTo
    {
        return $this->belongsTo(Currency::class, 'currency_id', 'id');
    }

The following code will not work

$price=Price::make(['pricing_schedule_id'=>1]);
dd($price->currency);

BadMethodCallException Method Illuminate\Database\Query\Grammars\MySqlGrammar::useLegacyGroupLimit does not exist.

The following code will work

$price=Price::make(['pricing_schedule_id'=>1]);
$price->exists=true;  /*Manually setting model as existing*/
dd($price->currency);

App\Models\Currency {#2159 ▼

fillable: array:6 [▶]

hidden: array:2 [▶]

connection: "mysql"

table: "currencies"

primaryKey: "id"

keyType: "int"

+incrementing: true

with: []

withCount: []

+preventsLazyLoading: false

perPage: 15

+exists: true +wasRecentlyCreated: false

attributes: array:12 [▶]

original: array:12 [▶]

changes: []

casts: array:1 [▶]

classCastCache: []

dates: []

dateFormat: null

appends: []

dispatchesEvents: []

observables: []

relations: []

touches: []

+timestamps: true

visible: []

guarded: array:1 [▶]

-joinRelationshipCache: []

globalHiddenAtrributes: array:1 [▶]

includeGlobalHiddenAttributes: true

humanFiendlyTimestampFormat: "F jS, Y, g:i A P"

excludedAttributes: []

auditEvent: null

forceDeleting: false

}

staudenmeir commented 3 years ago

In which model(s) are you using the HasEagerLimit trait?

navaneeth-raman-bhaskar commented 3 years ago

@staudenmeir none

Price model uses Staudenmeir\EloquentHasManyDeep\HasRelationships trait only

staudenmeir commented 3 years ago

What's the whole stacktrace of the exception?