lito
lito

Reputation: 3125

how to find, update and get back object collection in Laravel?

how to get the object back in Laravel when find and update, not just an integer?

$updatedCompProd = CompanyProduct::where('subscription_id', $stripeSub->id)
   ->update([
             'subscription_status'=>2,
             'subscription_end_datetime'=>Carbon::parse($stripeSub->current_period_end)->format('Y-m-d H:i:s')
]);

$updatedCompProd is an integer of value 1

Upvotes: 0

Views: 83

Answers (1)

mmabdelgawad
mmabdelgawad

Reputation: 2545

For the first issue you can use Laravel tap() helper function like so

tap(CompanyProduct::where('subscription_id', $stripeSub->id)->first(), function (CompanyProduct $companyProduct) use ($stripeSub) {
   $companyProduct->update([
       'subscription_status'=>2,
       'subscription_end_datetime'=>Carbon::parse($stripeSub->current_period_end)->format('Y-m-d H:i:s')
   ]);
});

it will return the updated object, you can read about Tap

Another Solution

$updatedCompProd = CompanyProduct::where('subscription_id', $stripeSub->id)->first();

$updatedCompProd->update([
             'subscription_status'=>2,
             'subscription_end_datetime'=>Carbon::parse($stripeSub->current_period_end)->format('Y-m-d H:i:s')
]);

dd($updatedCompProd); // it will work also,

Upvotes: 2

Related Questions