【发布时间】:2020-05-01 16:19:02
【问题描述】:
这是我问的最后一个问题..
我有一张会员表
Schema::create('role_memberships', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id');
$table->string('MembershipName');
$table->text('MembershipValue');
$table->timestamps();
});
我有两行具有相同 role_id 的数据,我想更新两行 这是我的控制器
$updateArray = [['MembershipValue' => $request->PostAdMaxImage],
['MembershipValue' => $request->PostAdExpiredDay]];
DB::table('role_memberships')->where('role_id', $id)-
>whereIn('role_id', $role->pluck('id'))->update($updateArray);
当我尝试更新时,我得到一个错误数组到字符串转换..
【问题讨论】:
-
你能发布你的错误吗?并且 $role->pluck('id') 返回数组,因为 whereIn 将数组作为 2 个参数。 ex (->whereIn('id', [1, 2, 3]))
-
@LorenzoBerti 我确实使用 ajax 进行了更新。所以,错误只显示在检查元素中
-
foreach($updateArray as $array){ DB::table('role_memberships')->where('role_id', $id)->whereIn('role_id', $role->pluck('id'))->update($array); } -
@SamuelJames 我使用了你给的方式,但问题是我的数据具有相同的值。例如:
id = 1, role_id = 1, I do $request->PostAdMaxImage = 'hello'id = 2, role_id = 1, I do $request->PostAdExpiredDay = 'world'。然后,我保存并再次重新打开..我的 PostAdMaxImage 和 PostAdExpiredDay 的值变成了“世界”,换句话说,它只取最后一个值 -
@abr 哦,我明白了.. 谢谢您的反馈;)