In Laravel I have a controller that take a request from ajax, and insert data in a DB table, the following code works good
在Laravel中,我有一个控制器,它接收来自ajax的请求,并将数据插入到DB表中,以下代码运行良好
-Laravel controller function
-Laravel控制器功能
public function updateFormcoords(Request $data, Form $form)
{
if ($data->ajax()){
$formcoord=new Formcoord;
$formcoord->field_name=$data->input('field_name');
$formcoord->x=$data->input('x');
$formcoord->y=$data->input('y');
$formcoord->w=$data->input('w');
$formcoord->h=$data->input('h');
$formcoord->r=$data->input('r');
$formcoord->shape=$data->input('shape');
$formcoord->fill=$data->input('fill');
$formcoord->q_id=$data->input('q_id');
$formcoord->q_option=$data->input('q_option');*/
$form->formcoords()->save($formcoord);
}
}
-the ajax request is the following:
- ajax请求如下:
$.ajax({
async: false,
url: {{ route(updateFormcoords) }},
headers: {"X-CSRF-TOKEN": token},
type: 'POST',
dataType: 'json',
data: {field_name:boxes[i].field_name, x: x, y: y, w: w, h: h, r: r, shape: boxes[i].shape, fill: boxes[i].fill.substring(1), multiMark: boxes[i].multiMark, q_id: boxes[i].q_id, q_option: boxes[i].q_option, _token: token}
});
-and finally the rout is the following
-最后的溃败如下。
Route::post('/createform/{form}/update', ['as' => 'updateFormcoords', 'uses' =>'FormController@updateFormcoords']);
All of this is working pretty good but what I would really want is to create all of the field with a $request->all(), so I won't need to write down field by field of the DB, in other words this is what I am trying to do:
所有这些都运行得很好但是我真正想要的是用$request-> All()创建所有的字段,所以我不需要逐个字段地写DB,换句话说,这就是我要做的:
public function updateFormcoords(Request $data, Form $form)
{
if ($data->ajax()){
$formcoord=new Formcoord;
$formcoord->create($data->all());
$form->formcoords()->save($formcoord);
}
}
but then a got the following error
然后a得到了下面的错误
"SQLSTATE[HY000]: General error: 1364 Field 'form_id' doesn't have a default value (SQL: insert into
formcoords
(field_name
,x
,y
,w
,h
,r
,shape
,fill
,q_id
,q_option
,updated_at
,created_at
) values (asdf, 0.08479087452471483, 0.018527667984189724, 0.09125475285171103, 0.06027667984189724, 0, 10, 91e57b, 0, 0, 2017-02-23 16:13:00, 2017-02-23 16:13:00))"“SQLSTATE[HY000]:一般错误:1364字段‘form_id’没有默认值(SQL:插入到formcoords (field_name, x, y, w, h, h, shape, fill, q_id, updated_at, created_at)”值
I don't understand because I thought Laravel eloquent was supposed to fill the form_id field automatically when saving the row in the parent DB table variable (in the code line “$form->formcoords()->save($formcoord)”. I appreciate a lot any help
我不理解,因为我认为Laravel的口才会自动地填入form_id字段,当它保存在父DB表变量中的行(在代码行“$form->formcoords()->save($formcoord)”中。非常感谢你的帮助
Best regards.
致以最亲切的问候。
1 个解决方案
#1
2
Don't do create, create will attempt to create the row, but you haven't associated it with any form yet.
不要创建,create将尝试创建行,但是您还没有将它与任何表单关联。
Do:
做的事:
$formcoord=new Formcoord($data->all());
$form->formcoords()->save($formcoord);
#1
2
Don't do create, create will attempt to create the row, but you haven't associated it with any form yet.
不要创建,create将尝试创建行,但是您还没有将它与任何表单关联。
Do:
做的事:
$formcoord=new Formcoord($data->all());
$form->formcoords()->save($formcoord);