Laravel 中保留旧值并选择多个选项
当前代码
<label class="form-label">Update Tags</label>
<select id="tags" name="tags[]" multiple class="form-control">
@foreach ($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
@endforeach
</select>
public function edit($aid, $id)
{
$account = Account::where('id', $aid)->first();
$friend = LineUser::where('id', $id)->first();
$tags = Tag::where('account_id', $aid)->get();
return view('dashboard.friends.edit', [
'friend' => $friend,
'account' => $account,
'tags' => $tags,
]);
}
public function update(Request $request, $aid, $id)
{
$request->validate([
'name' => 'required',
]);
// change date format
$DOB = $request->input('dob-year') . "/" . $request->input('dob-month') . "/" . $request->input('dob-day');
// sync updated tags
$data = [];
$data['tags'] = $request->input('tags');
LineUser::find($id)->tags()->sync($data['tags']);
// update records
LineUser::where('id', $id)
->update([
'name' => $request->input('name'),
'birthday' => $DOB,
'phone' => $request->input('phone'),
'postcode' => $request->input('postcode'),
'gender' => $request->input('gender'),
'email' => $request->input('email'),
]);
// session title
Session::put('title', 'User updated');
return redirect('accounts/' . $aid . '/' . 'friends' . '/' . $id . '/' . 'edit')
->with('message', 'User has been updated.');
我想要什么 是如果在旧记录中设置了值,则选择选择标记中的选项。
我尝试过的 有很多与此类似的答案,我尝试了大部分我认为可能有效的答案。以下是无效的方法。
@if (old('tags')==$tag->id)
<option value={{$tag->id}} selected>{{ $tag->name }}</option>
@else
<option value={{$tag->id}} >{{ $tag->name }}</option>
@endif
<option value="{{ $tag->id }}" {{ in_array($tag->id, (array) old('tags', [])) ? "selected" : "" }}>{{ $tag->name }}</option>
{{ (collect(old('tags'))->contains($tag->id)) ? 'selected':'' }}
我认为 old() 函数由于某种原因不起作用,但不确定这里到底出了什么问题。我有一个通过数据透视表连接的标签表和线路用户表。我也尝试过在 Javascript 中执行此操作,但我放弃了,因为我无法使用 javascript。如果有人可以帮助我解决这个问题。
Current code
<label class="form-label">Update Tags</label>
<select id="tags" name="tags[]" multiple class="form-control">
@foreach ($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
@endforeach
</select>
public function edit($aid, $id)
{
$account = Account::where('id', $aid)->first();
$friend = LineUser::where('id', $id)->first();
$tags = Tag::where('account_id', $aid)->get();
return view('dashboard.friends.edit', [
'friend' => $friend,
'account' => $account,
'tags' => $tags,
]);
}
public function update(Request $request, $aid, $id)
{
$request->validate([
'name' => 'required',
]);
// change date format
$DOB = $request->input('dob-year') . "/" . $request->input('dob-month') . "/" . $request->input('dob-day');
// sync updated tags
$data = [];
$data['tags'] = $request->input('tags');
LineUser::find($id)->tags()->sync($data['tags']);
// update records
LineUser::where('id', $id)
->update([
'name' => $request->input('name'),
'birthday' => $DOB,
'phone' => $request->input('phone'),
'postcode' => $request->input('postcode'),
'gender' => $request->input('gender'),
'email' => $request->input('email'),
]);
// session title
Session::put('title', 'User updated');
return redirect('accounts/' . $aid . '/' . 'friends' . '/' . $id . '/' . 'edit')
->with('message', 'User has been updated.');
What I want
is that the options in the select tag be selected if the value is set in old records.
What I tried
There were many answers on SO similar to this and I tried most of the ones I thought might work. Below are methods that did not work.
@if (old('tags')==$tag->id)
<option value={{$tag->id}} selected>{{ $tag->name }}</option>
@else
<option value={{$tag->id}} >{{ $tag->name }}</option>
@endif
<option value="{{ $tag->id }}" {{ in_array($tag->id, (array) old('tags', [])) ? "selected" : "" }}>{{ $tag->name }}</option>
{{ (collect(old('tags'))->contains($tag->id)) ? 'selected':'' }}
I think the old() function is not working for some reason but am not sure exactly what is wrong here. I have a tags table and a lineusers table that are connected via a pivot table. I also tried doing this in Javascript but I gave up cause Im not that able in javascript. If someone can help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以看看您的验证是如何进行的吗?
old() 函数不起作用的常见原因之一是验证后重定向时没有使用
->withInput()
。验证示例:
然后在 HTML 上,您可以在选项标记上添加条件
Can we see how your validation works?
One of common reasons why old() function is not working is because you're not using
->withInput()
when you are redirecting after validation.Example validation:
Then on your HTML, you can add condition on the option tag