如何在 Laravel 8 中存储多个 Select 值?

How to Store Multiple Select Values in Laravel 8?

所以我的问题是,如果我尝试只保存一个值,例如 post 的“布局中的位置”,它会起作用,但是当我保存一个数组时,我会得到类似 [数据库中的“值 1”、“值 2”],因此无法读取它,并且在 CRUD 控制器中,当我 editing.everything 时没有保存任何数据,除了我从数据中获取的值外,它工作得很好。我将不胜感激任何帮助、方法或替代方案

edit.blade.php

<div class="form-group">
                                <label>Select Post Location</label>
                                <select
                                    class="form-control select2 select2-hidden-accessible"
                                    multiple=""
                                    data-placeholder="Select Locations"
                                    style="width: 100%"
                                    tabindex="-1"
                                    aria-hidden="true"
                                    name="post_locations[]"
                                    id="post_locations"
                                >
                                    <option value="TopBox-GR" @if ($post->post_locations == "TopBox-GR") selected @endif>TopBox-GR</option>
                                    <option value="TopBox-C3" @if ($post->post_locations == "TopBox-C3") selected @endif>TopBox-C3</option>
                                    <option value="TopBox-C4" @if ($post->post_locations == "TopBox-C4") selected @endif>TopBox-C4</option>
                                </select>




</div>

家庭控制器

public function index()
{
    $posts = post::where([['status',1], ['post_locations','TopBox-GR']])->get();
    return view('user.blog',compact('posts'));
}

并查看:

        <div class="topbox-c4">
            @foreach ($posts as $post)
                <a href="{{ route('post',$post->slug) }}">
                <div class="image-topbox-c4-container">
                    <img src="{{ Storage::disk('local')->url($post->image)}}" />
                </div>
                <div class="text-topbox-c4-container">
                    <h3>{{$post->title}}</h3>
                    <p>
                        {{$post->subtitle}}
                    </p>
                </div>
                </a>
            @endforeach
        </div>

post.controller:

public function update(Request $request, $id)
{
    $this->validate($request,[
        'title'=>'required',
        'subtitle'=>'required',
        'slug'=>'required',
        'body'=>'required',
        'image'=>'required',
        'post_locations'=>'required',
    ]);

    if($request->hasFile('image'))
    {
        $imageName = $request->image->store('public');
    }

    $post = post::find($id);
    $post->image = $imageName;
    $post->title = $request->title;
    $post->subtitle = $request->subtitle;
    $post->slug = $request->slug;
    $post->post_locations = $request->post_locations;
    $post->body = $request->body;
    $post->status = $request->status;
    $post->tags()->sync($request->tags);
    $post->categories()->sync($request->categories);

    $post->save();

    return redirect(route('post.index'));
}

首先,您应该在 $selectLocations 变量中获取选定的位置,并在 $allLocations 变量中获取 edit.blade.php

的所有位置

在edit.blade.php中可以这样显示:-

@foreach($selectLocations as $value)
<select class="form-control" name="post_locations[]" multiple=''>
    <option value="">Select Location</option>
    @foreach($allLocations as $val)
        <option @if($value['id'] == $val->id) {{ 'selected' }} @endif value="{{ $val->id }}">{{ $val->name}}</option>
    @endforeach
</select>
@endforeach