2013-10-01 27 views
0

我檢索從視圖中JSON:Laravel通過AJAX保存檢索到的值以db

$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('/', '[email protected]'); 
// here i wanna save the retrieved json 
Route::post('/', '[email protected]'); 

現在想保存所檢索的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 

這給出了一個錯誤,導致IM在postSave()FUNC做一些wron

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 
    } 
+0

什麼是你的表的字段名,並在現場你有''DKT000142''? – devo

+0

in No_這也是主鍵 – user2834172

+0

通過一目瞭然,它看起來像你發佈的數據和'Input :: get('Name');數組數組''將爲空。我建議看看瀏覽器中的XHR請求(ajax)。對於chrome:ctrl + shit + i,轉到網絡選項卡並檢查您的請求。 –

回答

0

解決方案1 ​​ - >如果使用在那裏,然後利用更新

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

解決方案2 - >如果使用find,然後用節省

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

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

這個getOne函數,是一個測試函數工程..但我想保存檢索JSON的價值。不是c-> name = amann informatik而是我想要c-> name = Input :: get('Name'); – user2834172

+0

嘗試類似更新的答案。 – devo

+0

同樣的問題... – user2834172