I have an input form named category that can have multiple value just like tags input form in stackoverflow when you asking a question
I want to get all input value from that form and insert it into category_service pivot table
I use $category = implode(",", Input::get('category')); to get the array value
And then i get Invalid argument supplied for foreach() error when try to insert it using this code:
foreach ($category as $category) {
DB::insert('INSERT INTO category_service (category_id, service_id) VALUES (?,?)', array('$category', $service->id));
}
the tables look like this:
category_table
+----+--------------+
| id | category |
+----+--------------+
| 1 | category_1 |
| 2 | category_2 |
+----+--------------+
service_table
+----+--------------+
| id | service |
+----+--------------+
| 1 | service_1 |
+----+--------------+
category_service_table //pivot table to store category id and service id
+----+--------------+-------------+
| id | category_id | service_id |
+----+--------------+-------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
+----+--------------+-------------+
the var_dump result is string(3) "2,1"
Categorydump into your question,