harunB10 harunB10 - 1 year ago 64
MySQL Question

How to increment MySQL field by one if one or more items are checked in form?

I have a page where user can vote which gaming platform (for example) is the best. In MySQL I have a table of these items. I want to update 'count' field and set it to +1 every time when specified item is checked.
After form is submitted I get an array of selected items:

array [
0 => "XBOX"
1 => "PS4"
2 => "PC"

Now, I want to check and compare this array with MySQL fields and increase their count values by 1.
I am trying to compare array with MySQL with this, but unsuccessfully.

$value = Input::get('platform'); //this produces presented array
$test = Platform::where('name', 'LIKE', implode(" ", $value))->get();

Answer Source

I think you should use whereIn instead of where

$test = Platform::whereIn('name', $value)->get();


You can do this by DB::raw():

   ->whereIn('name', $value)
       'count' => DB::raw('count + 1')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download