Laravel通过db中的ajax保存检索到的值

时间:2013-10-01 12:35:23

标签: jquery ajax json laravel laravel-4

我从视图中检索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');

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

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
  }

1 个答案:

答案 0 :(得分: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 - >如果使用find,则使用save

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

    $c = Customer::find('DKT000142');
    $c->Name = $input;
    $c->save();
    return Input::all();
}