0

我从视图中检索 json:

$container.parent().find('button[name=save]').click(function () {
    alert('we are trying to save');
  $.ajax({
    url: "/laranav/public/",
    data: {"data": handsontable.getData()}, //returns all cells' data
    dataType: 'json',
    type: 'POST',
    success: function (res) { console.log(res); alert(res); }
   /* error: function () {
      $console.text('Save error. POST method is not allowed on GitHub Pages. Run this example on your own server to see the success message.');
    }*/
  });

然后在Routes.php

// here i make the view
Route::get('/', 'CustomerController@getIndex');
// here i wanna save the retrieved json
Route::post('/', 'CustomerController@postSave');

现在想将检索到的 json 保存在 Controller 中:

public function postSave() {
    $t = Input::get('Name');
    $c = Customer::find('DKT000142');
    $c->Name = $t;
    $c->save();
}

检索 json 的东西看起来像:

 data[0][Name]:Afrifield Corporation
 data[0][City]:Maidstone
 data[1][Name]:Amann Informatik AG
 data[1][City]:Reinach BL
 data[2][Name]:Antarcticopy
 data[2][City]:Antwerpen
 data[3][Name]:Autohaus Mielberg KG
 data[3][City]:Hamburg 36

这给出了一个错误,导致我在 postSave() 函数中做错了

POST http://127.0.0.1/laranav/public/ 500 (Internal Server Error) 
send 
x.extend.ajax
(anonymous function) 127.0.0.1/laranav/public/:77
x.event.dispatch
v.handle

为了测试,我在这里更改了值,并且它以这种方式工作

public function getOne() {
    $c = Customer::find('DKT000142');
    $c->Name = 'Amann Informatik AG';
    $c->save();
}

我的餐桌课

 class Customer extends Eloquent {

     //The database table used by the model.
     protected $table = '7_0 - CRONUS (ai-Patches) AG$Customer';
     public $timestamps = false;
     public $primaryKey = 'No_';
     //important columns No_ | Name | City and a lot other that i don't wanna touch
  }
4

1 回答 1

0

解决方案 1 -> 如果使用 where,则使用 update

public function postSave() {
    $input = Input::all();
    $input = $input['data'][2]['Name'];
    Customer::where('No_','=','DKT000142')->update(array('Name' =>  $input));
    return Input::all();
}

解决方案 2 -> 如果使用查找,则使用保存

public function postSave() {
    $input = Input::all();
    $input = $input['data'][2]['Name'];

    $c = Customer::find('DKT000142');
    $c->Name = $input;
    $c->save();
    return Input::all();
}
于 2013-10-01T12:50:13.300 回答