Xerukm Xerukm - 6 months ago 40
PHP Question

Laravel 5.2 - Update previous query

I have this query:

Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->update(array(
'status' => 'stopped',
));


The problem is that the amount of records it has to go through to do the update causes it to take around 15 minutes or so to finish.

I would like to split it up so the select and update queries are separate entities. Something sort of like this:

$pending = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();

$pending->update(array(
'status' => 'stopped',
));


How would I go about doing this? Or is there an easier way?

Thanks!

Answer

I wasn't thinking, I figured out the answer. I had to run the second part in a foreach like so:

$records = Sendqueue::select()
            ->where('userID', Session::get('uid'))
            ->where('campaign', $id)
            ->where('status', 'pending')
            ->get();

foreach ($records as $record) {
        DB::table('sendqueue')
            ->where('ID', $record->ID)
            ->update(['status' => 'stopped']);
}