0

複数のレコードを取得するときに、保存または更新しようとしていますが、機能しません..

これを行うと、最初はうまくいったように見えます:

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

しかし、リフレッシュしてそのようにテストした後でも、古い名前が表示されます。

$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

この取得した配列で更新したい列 Name と City もある場合..

したがって、最後の質問には、次よりも速い解決策はありません。

    $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 です。これもモデル クラスで設定します。

4

2 に答える 2

0
$c[0]->name = "New name";
$c[0]->save();
于 2013-10-02T09:14:37.257 に答える
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);
}
于 2013-10-02T09:09:55.037 に答える