在表单中包含数组和将其发布之间的区别
Difference between having array inside form and posting it
这是我的数组
$datas = array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4'));
当我 return 我得到的是
return $data;
输出:
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
我想从邮递员那里得到同样的数组
所以,我将名称的输出粘贴为数据
我收到的是
$gotdata = Input::get('data');
当我打印时我得到了相同的输出
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
当我尝试保存记录时,$data 有效
MYModel::insert($data);
但是
MTIServiceAttendance::insert($gotdata);
并抛出错误
Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given
我该如何解决这个问题才能保存 $gotdata。
注意:对于模型,输入应为
MTIServiceAttendance::insert(array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4')));
我应该怎么做才能使输入表单像这个数组一样?
更新:这是数组的 var_dump
Return :
return var_dump($data);
输出:
array(2) { [0]=> array(3) { ["studentid"]=> string(1) "9" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } [1]=> array(3) { ["studentid"]=> string(2) "10" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } }
Return :
return var_dump($gotdata);
输出:
string(94) "[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]"
$gotdata
是数组的 JSON 字符串表示。因此,虽然它在您的第一个输出中看起来相同,但您可以清楚地看到使用 var_dump
时的差异。只需使用 json_decode
将其转换为数组:
$gotdata = Input::get('data');
$gotdata = json_decode($gotdata, true);
MTIServiceAttendance::insert($gotdata);
这是我的数组
$datas = array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4'));
当我 return 我得到的是
return $data;
输出:
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
我想从邮递员那里得到同样的数组
所以,我将名称的输出粘贴为数据
我收到的是
$gotdata = Input::get('data');
当我打印时我得到了相同的输出
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
当我尝试保存记录时,$data 有效
MYModel::insert($data);
但是
MTIServiceAttendance::insert($gotdata);
并抛出错误
Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given
我该如何解决这个问题才能保存 $gotdata。
注意:对于模型,输入应为
MTIServiceAttendance::insert(array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4')));
我应该怎么做才能使输入表单像这个数组一样?
更新:这是数组的 var_dump
Return :
return var_dump($data);
输出:
array(2) { [0]=> array(3) { ["studentid"]=> string(1) "9" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } [1]=> array(3) { ["studentid"]=> string(2) "10" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } }
Return :
return var_dump($gotdata);
输出:
string(94) "[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]"
$gotdata
是数组的 JSON 字符串表示。因此,虽然它在您的第一个输出中看起来相同,但您可以清楚地看到使用 var_dump
时的差异。只需使用 json_decode
将其转换为数组:
$gotdata = Input::get('data');
$gotdata = json_decode($gotdata, true);
MTIServiceAttendance::insert($gotdata);