2013-10-02 32 views
0

我試圖保存或更新,retrivieng複式記錄時,卻無法得到它的工作..保存在Laravel檢索時多條記錄

做這件事時,起初它看起來像它的工作:

$c = Customer::select('Name', 'City'); 
$c[0]->update(array('Name' => 'new Name')); 
var_dump($c[0]['Name']); 

但在刷新後,並測試它這樣,我仍然看到使用oldName:

$c = Customer::select('Name', 'City'); 
var_dump($c[0]['Name']); 

有人知道我在做什麼的青蛙?

我有檢索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 

如果也有,我想這個檢索陣列..

所以最後一個問題,更新的列名與城市有沒有更快的解決方案比:

$custNo = Customer::select('Name', 'No_')->get(); 

    for ($i = 0; $i < count($custNo); $i++) { 
     $c = Customer::select('Name', 'City')->where('No_','=',$custNo[$i]->No_); 
     $c->update(array('Name' => $input['data'][$i]['Name'])); 
    } 

NO_是我的PrimaryKey我還設置這個模型中的類..

回答

0

而不是get()$c[0],請嘗試使用first()。更新後您還需要重新提取。它是對象,而不是數組。所以$c['Name']將不會工作,除非你改變它爲一個數組。

$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get(); 
foreach ($customers as $customer) { 
    $customer->update(array('Name' => 'new Name')); 
    //you need to re-fetch for the updated data, assuming you have an id field 
    $currentCustomer = Customer::find($customer->id); 
    echo var_dump($currentCustomer->Name); 
} 

編輯:也大規模更新,你可以嘗試這樣的:

$update = Customer::where('No_','=','DKT000142')->update(array('Name' => 'new Name')); 

EDIT2:爲了既獲得大規模更新並獲得更新的記錄,這是我能想到的最好的辦法:

//First, we mass-update 
$update = Customer::where('No_','=','DKT000142')->update(array('Name' => 'new Name')); 

//Then we get the updated data and loop: 
$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get(); 
foreach ($customers as $customer) { 
    echo var_dump($currentCustomer->Name); 
} 
+0

但這樣我才撿回了第一條記錄,我想檢索多個記錄..看到更新的問題 – user2834172

+0

@ user2834172更新了我對循環情況的回答。我假設你有一個客戶表的'id'字段主鍵。 – Arda

+0

更新的問題,如果你想澄清我的最後一部分?但它現在有效,但我認爲應該有更好的方法來做到這一點... – user2834172

0
$c[0]->name = "New name"; 
$c[0]->save(); 
+0

給出錯誤,因爲它是一個不是數組的對象... – user2834172