2013-10-02 33 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