foreach ($category as $category) {
DB::insert('INSERT INTO category_service (category_id, service_id) VALUES (?,?)', array('$category', $service->id));
}
テーブルは次のようになります。試してみるには、このコードを使用してそれを挿入するとき、私は私がInvalid argument supplied for foreach()
エラーを取得
そして配列値を取得するために$category = implode(",", Input::get('category'));
を使用
category_service
ピボットテーブルにそれを挿入します。
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 |
+----+--------------+-------------+
var_dump
結果はPHP implode
機能あなたは文字列に配列を結合し、その結果Invalid argument supplied for foreach()
エラーとして、foreachのために文字列を渡すとstring(3) "2,1"
を試してみてください。また 'Category'ダンプをあなたの質問に追加します。 – Qazi