2

Actualy,my scenario is user send request to another user by paying coins,If user doesn't accept the request below 72 hours the coins should be revered to the user,

Im storing coins in coins table.

How can I update records if records are older than 72 hours,I have tried with below code

public function getAllRequests(Request $request)
{
    $expired_details = MenterRequest::where('created_at', '<', Carbon::now()->subHours(72)->toDateTimeString())->get();
    foreach($expired_details as $expired)
    {
        $msubIds = $expired->menter_subscriber_id;
        $update =Coins::where('user_id','=',$msubIds)->update([
                        'mcoins','=>','2000001'
                    ]);
    }
}

I can get rows older than 72 hour,now how can update them

  • 1
    try this `foreach($expired_details as $expired) { $expired->mcoins = '2000001'; $expired->save(); }` – Maraboc Nov 22 '17 at 12:59
  • 1
    Possible duplicate of [Eloquent model mass update](https://stackoverflow.com/questions/22430716/eloquent-model-mass-update) – ceejayoz Nov 22 '17 at 12:59
  • hello @Maraboc my question is how to update rows which are older than 72 hours. –  Nov 22 '17 at 13:01
  • 1
    Yes i know you get the records older than 72 hours by this query `$expired_details = MenterRequest::where('created_at', '<', Carbon::now()->subHours(72)->toDateTimeString())->get();` and my code is the update of the returned records no ?? – Maraboc Nov 22 '17 at 13:04
  • Yes that worked thank you @Maraboc –  Nov 23 '17 at 05:57

1 Answers1

4

You can use the same foreach loop to update all the expired rows.

foreach($expired_details as $expired)
{
    $msubIds = $expired->menter_subscriber_id;
    $update =Coins::where('user_id','=',$msubIds)->update([
                    'mcoins','=>','2000001'
   ]);
    /* Update your rows and save the data */
    $mentor_update = MenterRequest::find($expired->mentor_id);
    $mentor_update->your_column = your_data;
    $mentor_update->save();
}

Hope this helps. :)