Support the ongoing development of Laravel.io →
posted 5 years ago
Eloquent

When Call ->first() which returns record, then call ->delete(), then call ->first() again, the ->first() still return the deleted record (some how it was not cleaned in the cache, while ->get() does return empty set.

>>> $p->subscription('listing')->usage()->where('feature_id',3)->first();
=> Rinvex\Subscriptions\Models\PlanSubscriptionUsage {#3802
     id: 6,
     subscription_id: 1,
     feature_id: 3,
     used: 1,
     valid_until: null,
     timezone: null,
     created_at: "2019-11-30 21:17:52",
     updated_at: "2019-11-30 21:17:52",
     deleted_at: null,
   }
>>> $p->subscription('listing')->usage()->where('feature_id',3)->delete();
=> 1
>>> $p->subscription('listing')->usage()->where('feature_id',3)->first();
=> Rinvex\Subscriptions\Models\PlanSubscriptionUsage {#3828
     id: 6,
     subscription_id: 1,
     feature_id: 3,
     used: 1,
     valid_until: null,
     timezone: null,
     created_at: "2019-11-30 21:17:52",
     updated_at: "2019-11-30 21:17:52",
     deleted_at: null,
   }
>>> $p->subscription('listing')->usage()->where('feature_id',3)->get();
=> Illuminate\Database\Eloquent\Collection {#3858
     all: [],
   }

Also ->unsetRelation() doesn't help (although I think this should be supported without them)

>>> $p->subscription('listing')->unsetRelation('usage');
=> Rinvex\Subscriptions\Models\PlanSubscription {#3670
     id: 1,
     user_type: "App\Models\Property",
     user_id: 100,
     plan_id: 3,
     slug: "listing",
     name: "{"he":"listing"}",
     description: null,
     trial_ends_at: "2019-11-30 13:36:19",
     starts_at: "2019-11-30 21:14:51",
     ends_at: "2019-12-30 21:14:51",
     cancels_at: null,
     canceled_at: null,
     timezone: null,
     created_at: "2019-11-29 13:36:19",
     updated_at: "2019-11-30 21:14:51",
     deleted_at: null,
   }
>>> $p->subscription('listing')->usage()->where('feature_id',3)->first();
=> Rinvex\Subscriptions\Models\PlanSubscriptionUsage {#3702
     id: 7,
     subscription_id: 1,
     feature_id: 3,
     used: 1,
     valid_until: null,
     timezone: null,
     created_at: "2019-11-30 21:36:47",
     updated_at: "2019-11-30 21:36:47",
     deleted_at: null,
   }
>>> $p->subscription('listing')->usage()->where('feature_id',3)->get();
=> Illuminate\Database\Eloquent\Collection {#3732
     all: [],
   }

Also ->fresh() doesn't help (although I think this should be supported without them)

>>> $p2 = $p->subscription('listing')->usage()->where('feature_id',3)->first();
=> Rinvex\Subscriptions\Models\PlanSubscriptionUsage {#3745
     id: 7,
     subscription_id: 1,
     feature_id: 3,
     used: 1,
     valid_until: null,
     timezone: null,
     created_at: "2019-11-30 21:36:47",
     updated_at: "2019-11-30 21:36:47",
     deleted_at: null,
   }
>>> $p2->refresh();
Illuminate/Database/Eloquent/ModelNotFoundException with message 'No query results for model [Rinvex/Subscriptions/Models/PlanSubscriptionUsage] 7'
>>> $p2 = $p->subscription('listing')->usage()->where('feature_id',3)->first();
=> Rinvex\Subscriptions\Models\PlanSubscriptionUsage {#3768
     id: 7,
     subscription_id: 1,
     feature_id: 3,
     used: 1,
     valid_until: null,
     timezone: null,
     created_at: "2019-11-30 21:36:47",
     updated_at: "2019-11-30 21:36:47",
     deleted_at: null,
   }
>>>
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Avi Sev avictory6 Joined 11 Aug 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.