作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在表之间有一个连接,然后我希望更改 2 个值并使用此连接保存每条记录。
foreach ($data as $datas) {
$datas->dealerCode = $datas->dealer;
$datas->owner = 'K';
$datas->save();
}
我能够获取集合中的记录 ($data
) 但似乎无法保存。我要更新的字段在模型中是可填写的。
我错过了什么?
谢谢。
$数据是;
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'
这是我的 Controller 的完整功能;
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();
}
}
最佳答案
你必须在循环的每一步创建新对象
foreach ($data as $datas) {
$datas = new Datas();
$datas->dealerCode = $datas->dealer;
$datas->owner = 'K';
$datas->save();
}
关于php - 为什么这个 foreach 不保存?拉维尔 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878368/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!