imbayago imbayago - 1 year ago 153
MySQL Question

Lavarel 5.2 Conditional Update to Database

How do I update a database column based on a value from a different column?

I have a query like the following which pulls current usage (example: 200):

$currentusage = CurrentPercentageModel::select('current_usage')->where('name', '=', 'User Name')->get();

I would like to update the same value +1 after the each time it passes certain conditions.

query would look like this and adds a new row to the table with the new value (example: 201):

$currentusage = $currentusage + 1;
$savecurrentusage = new CurrentPercentageModel;
$savecurrentusage->current_usage = $currentusage;

How do I update the same value from the one I pull from my query?

Answer Source

A bit messy but should works

$model = CurrentPercentageModel::where('name', '=', 'User Name')->first();
$model->current_usage += 1;