为什么这个foreach不能保存? Laravel 5

时间:2016-01-19 13:42:05

标签: php laravel laravel-5

我在表之间有联接,然后我希望更改2个值并使用此联接保存每个记录。

foreach ($data as $datas) {
     $datas->dealerCode = $datas->dealer;
     $datas->owner = 'K';

     $datas->save();
}

我能够获取集合中的记录($data),但似乎无法保存。我想要更新的字段可以在模型中填写。

我错过了什么?

感谢。

$ datas is;

 Collection {#446 ▼
      #items: array:12 [▼
    0 => Consumer {#447 ▼
      #fillable: array:35 [▶]
      #guarded: array:2 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:59 [▼
        "id" => 512
        "pin" => 0
        "campaignCode" => "Test"
        "campaignId" => 7
        "eventCode" => "Test"
        "eventLocation" => "Testville"
        "rsvp" => 0
        "attendance" => 1
        "appended" => 0
        "salutation" => "Mr."
        "firstName" => "K E"
        "lastName" => "Test"
        "email" => "test@test.com"
        "postal" => "x1x1x1"
        "address1" => "7493 Amber Grounds"
        "address2" => ""
        "city" => "Hardscrabble"
        "province" => "MB"
        "language" => "en"
        "dealerCode" => ""
        "created_at" => "2015-09-11 14:20:36"
        "updated_at" => "2016-01-18 15:38:53"
        "dealer" => "11111"
      ]
      #original: array:59 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Consumer {#448 ▶}
    2 => Consumer {#449 ▶}
    3 => Consumer {#450 ▶}
    4 => Consumer {#451 ▶}
    5 => Consumer {#452 ▶}
    6 => Consumer {#453 ▶}
    7 => Consumer {#454 ▶}
    8 => Consumer {#455 ▶}
    9 => Consumer {#456 ▶}
    10 => Consumer {#457 ▶}
    11 => Consumer {#458 ▶}
  ]
}

print_r($ datas)给了我;

App\Consumer Object
(
[fillable:protected] => Array
    (
        [0] => salutation
        [1] => firstName
        [2] => lastName
        [3] => email
        [4] => postal
        [5] => phone
        [6] => address1
        [7] => address2
        [8] => city
        [9] => province
        [24] => dealerCode
    )

[guarded:protected] => Array
    (
        [0] => id
        [1] => created_at
    )

[connection:protected] => 
[table:protected] => 
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
    (
        [id] => 512
        [pin] => MA61151A7
        [campaignCode] => 101400101
        [campaignId] => 7
        [eventCode] => Test
        [eventLocation] => Testville
        [rsvp] => 0
        [attendance] => 1
        [appended] => 0
        [salutation] => Mr.
        [firstName] => K E
        [lastName] => Test
        [email] => test@test.com
        [postal] => X1X1X1
        [address1] => 7493 Amber Grounds
        [address2] => 
        [city] => Hardscrabble
        [province] => MB
        [language] => en
        [dealerCode] => 
        [dealer] => 11111
    )

[original:protected] => Array
    (
        [id] => 512
        [pin] => MA61151A7
        [campaignCode] => 101400101
        [campaignId] => 7
        [eventCode] => Test
        [eventLocation] => Testville
        [rsvp] => 0
        [attendance] => 1
        [appended] => 0
        [salutation] => Mr.
        [firstName] => K E
        [lastName] => Test
        [email] => test@test.com
        [postal] => X1X1X1
        [address1] => 7493 Amber Grounds
        [address2] => 
        [city] => Hardscrabble
        [province] => MB
        [language] => en
        [dealerCode] => 
        [dealer] => 11111
    )

消费者模型;

protected $fillable = [
    'salutation',
    'firstName',
    'lastName',
    'email',
    'postal',
    'phone',
    'address1',
    'address2',
    'city',
    'province',
    'owner',
];

顺便说一句,这就是它在mySql中的工作方式;

update consumers c
join owners o
on c.email = o.email
set c.owner = 'K'

以下是我的控制器的完整功能;

public function setOwner()
{
        $data = Consumer::join('owners', 'consumers.email', '=', 'owners.email')
            ->where('consumers.eventCode', '=', 'Test')->get();

    foreach ($data as $datas) {
        $obj = Consumer::find($datas['id']);
        //$obj = empty($obj) ? new Consumer : $obj;
        // $obj = new Consumer;
        $obj->dealerCode = $datas['dealer'];
        $obj->owner = 'K';
        $obj->save();
    }

}

3 个答案:

答案 0 :(得分:2)

你必须在循环的每一步中创建新对象

foreach ($data as $datas) {
        $datas = new Datas();
        $datas->dealerCode = $datas->dealer;
        $datas->owner = 'K';
        $datas->save();
    }

答案 1 :(得分:0)

foreach ($data as &$datas) {
        $datas->dealerCode = $datas->dealer;
        $datas->owner = 'K';

        $datas->save();
    }

刚刚通过&在$ database之前。 $ data将被更新。

答案 2 :(得分:0)

试试这段代码: -

foreach ($data as $datas) {
     $obj = Consumer::find($datas['id']);
     //$obj = empty($obj) ? new Consumer : $obj;
     // $obj = new Consumer;
     $obj->dealerCode = $datas['dealer'];
     $obj->owner = 'K';
     $obj->save();
}

希望它对您有用:)