我有一个foreach循环来获取数据库中存储的值并显示它们中的每一个,它工作正常,但现在我想批量更新它的值,这是我生成复选框的代码。
@foreach ($servers as $server)
<input type="checkbox" name="servidor[]" value="status_on" {{$server->status == 1 ? 'checked' : ''}}>
@endforeach
这是我更新数据库值的代码。
foreach (request('servidor') as $update) {
DB::table('servers')
->where('id', $update)
->update(['status' => $update->servidor]);
}
它不起作用,我一直在:
尝试获取非对象的属性
在这一行:->update(['status' => $update->servidor]);
如何批量更新这些动态生成的复选框?
答案 0 :(得分:1)
如果您想获取server_id =&gt; value数组,则应将输入复选框更改为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Click!"
android:textAllCaps="false"/>
</LinearLayout>
所以你可以制作服务器id-&gt; value数组,然后使用foreach($ value as $ key =&gt; $ value),你的问题就会解决。
答案 1 :(得分:0)
将对象$ update的值打印为
print_r(update) or var_dump(update) to see the $update objects
然后将该行更改为
>update(['status' => $update->CORRECT-ITEM-FROM-ABOVE-PRINT-OUT]);
ps:回顾上面的代码看起来逻辑错了。 你想把'server-&gt; status'设置为“someValue”,并在那个循环中“someValue”是错误的。
->update(['status' => $update->servidor]);