在数据库中存储复选框值时出现问题



我正在尝试将复选框值存储在数据库中。我有这三张桌子。

字段

id    name
1     gender
2     looking_for

field_values(此处field_id引用字段表上的id(

id    field_id    value    label
1        1          1      Men
2        1          2      Women
3        2          3      Relationship
4        2          4      Friendship
5        2          5      Marriage

user_interest(此处field_id引用field_values表上的field_id,value_id引用field_values表格上的值(

user_id    field_id    value_id
1          1           2
1          2           4
1          2           5

blade中的gender使用选项值,looking_for使用复选框值。我做了一个函数,试图更新这两个函数。我在函数中使用了两个前臂,我能够成功更新性别选项,但无法更新looking_for选项。当我点击提交按钮时,什么都不会发生,当我在foreach中转储任何应该更新复选框的内容时,它不会转储。非常感谢您的帮助。这是我的密码。

web.php

Route::patch('profile/interests', 'UserProfileController@updateInterestsData')->name('profile.update.interests.data');

UserProfileController.php

public function updateInterestsData(UpdateInterestsDataRequest $request)
{
$user = User::with('userProfile')->where('id', Auth::user()->id)->firstOrFail();
$request->validated();
$userId = $request->input('user_id') ? $request->input('user_id') : Auth::user()->id;
$data = $request->all();
$options = [
'gender'         => 1,
'looking_for'    => 2
];
foreach ($options as $fieldName => $fieldId) {
if (! empty($data[$fieldName])) {
DB::table('user_interests')
->where('user_id', $userId)
->where('field_id', $fieldId)
->delete();
if (is_array($data[$fieldName])) { // CHECKBOX FIELDS AND HERE IT DOESN'T WORK!!!
//dd('DIE!!!!!!') IT DOESN'T ENTER HERE!!!
foreach ($data[$fieldName] as $key => $value) {
DB::table('user_interests')->insert([
'user_id'  => $userId,
'field_id' => $fieldId,
'value_id' => $value
]);
}
} else { // SELECT FIELDS!!!
DB::table('user_interests')->insert([
'user_id'  => $userId,
'field_id' => $fieldId,
'value_id' => $data[$fieldName]
]);
}
}
}
$user->userProfile->update(
[
'age_from_preference' => $request->age_from_preference,
'age_to_preference' => $request->age_to_preference,
'updated_at' =>  Carbon::now()
]
);
$request->user()->save();
return redirect()->route('profile.show', [$user->username]);
}

index.blade.php

<form action="{{ route('profile.update.interests.data') }}" method="POST" class="flex">
@method('PATCH')
@csrf
<div class="form-group">
<span>Interessiert an</span>
{{-- wrong value - selected --}}
@isset($options)
@foreach($options as $name => $fieldData)
@if ($name == 'gender')
<div class="selectHolder">
<select name="{{ $name }}">
<option selected="true" disabled="disabled" value="" style="display:none">bitte auswählen</option>
@foreach($fieldData['data'] as $value => $label)
<option value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'selected' : '') : '' }}>
{{ $label }}
</option>
@endforeach
</select>
</div>
<?php
unset($options[$name]);
?>
@endif
@endforeach
@endisset
</div>
<div class="form-group">
<span>Im Alter von</span>
<input type="text" placeholder="XX" maxlength="2" value="{{ $userForShowProfile->userProfile->age_from_preference ?? "" }}" name="age_from_preference">
<span>Jahren bis</span>
<input type="text" placeholder="XX" maxlength="2" value="{{ $userForShowProfile->userProfile->age_to_preference ?? "" }}" name="age_to_preference">
<span>Jahren</span>
</div>
{{-- wrong value - checked --}}
@isset($options)
<div class="form-group flex mt-5">
@foreach($options as $name => $fieldData)
@if ($name == 'looking_for')
@foreach ($options[$name]['data'] as $value=>$label)
<div class="interestedIn">
<input type="checkbox" name="{{ $name.'-'.$value }}" value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'checked' : null) : '' }}>
<label for="{{$name}}-{{ $value }}">{{ $label }}</label>
</div>
@endforeach
@endif
@endforeach
</div>
@endisset
<div class="form-group">
<label for="" class="textBold">Button</label>
<input type="submit" class="form-control" name="submit" value="BUTTON">
</div>
</form>

$options变量的代码

public static function getProfileLookingForDisplayOptions()
{
$options = [
'gender' => ['id' => 1, 'label' => "Interessiert an"],
'looking_for' => ['id' => 2, 'label' => ""]
];
$data_options = [];
foreach ($options as $field => $value) {
$data_options[$field]['data'] = Value::fieldValues($field);
$data_options[$field]['label'] = $options[$field];
if (!in_array($field, ['gender', 'looking_for'])) {
$data_options[$field]['data'][100] = "Doesn't matter";
}
}
//dd($data_options);
return $data_options;
}

如果我正确理解您的问题,要处理PHP上的多个复选框,您需要将[]添加到其name属性中。这样一来,PHP就知道应该将这些值解释为数组。

此外,您的输入名称与$data[$fieldName]不匹配。

这样做:

@isset($options)
<div class="form-group flex mt-5">
@foreach($options as $name => $fieldData)
@if ($name == 'looking_for')
@foreach ($options[$name]['data'] as $value=>$label)
<div class="interestedIn">
<input type="checkbox" name="{{ $name }}[]" value="{{ $value }}" {{ isset($data[$fieldData['label']['id']]) ? (in_array($value, $data[$fieldData['label']['id']]) ? 'checked' : null) : '' }} id="{{$name}}-{{ $value }}">
<label for="{{$name}}-{{ $value }}">{{ $label }}</label>
</div>
@endforeach
@endif
@endforeach
</div>
@endisset

最新更新