harunB10 harunB10 - 3 months ago 8
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

I think you should use whereIn instead of where

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

Update:

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

DB::table('Platforms')
   ->whereIn('name', $value)
   ->update([
       'count' => DB::raw('count + 1')
   ]);
Comments