Laravel 中保留旧值并选择多个选项

发布于 2025-01-11 17:39:00 字数 2513 浏览 0 评论 0原文

当前代码

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-01-18 17:39:00

我们可以看看您的验证是如何进行的吗?

old() 函数不起作用的常见原因之一是验证后重定向时没有使用 ->withInput()

验证示例:

public function saveData(Request $request)
{
    // Validations go here

    if (!isValid()) {
        // If validation fails, using ->withInput(),
        // redirect back to the view with the data you've sent here
        return redirect()->back()->withInput()->withErrors('Error Validation');
    }

    return redirect()->back()->with('message','Successful');
}

然后在 HTML 上,您可以在选项标记上添加条件

<label class="form-label">Update Tags</label>
<select id="tags" name="tags" multiple class="form-control">
@foreach ($tags as $tag)
    <!-- Add condition here to add attribute like 'selected' -->
    <option value="{{ $tag->id }}" @if($tag->id == old('tags')) selected @endif>{{ $tag->name }}</option>
@endforeach
</select>

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:

public function saveData(Request $request)
{
    // Validations go here

    if (!isValid()) {
        // If validation fails, using ->withInput(),
        // redirect back to the view with the data you've sent here
        return redirect()->back()->withInput()->withErrors('Error Validation');
    }

    return redirect()->back()->with('message','Successful');
}

Then on your HTML, you can add condition on the option tag

<label class="form-label">Update Tags</label>
<select id="tags" name="tags" multiple class="form-control">
@foreach ($tags as $tag)
    <!-- Add condition here to add attribute like 'selected' -->
    <option value="{{ $tag->id }}" @if($tag->id == old('tags')) selected @endif>{{ $tag->name }}</option>
@endforeach
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文