Skip to content
Advertisement

How to Store Multiple Select Values in Laravel 8?

So my problem is that if i try to save only one value like “Location in the layout” of a post it works, but the moment i am saving an array I’m getting something like [“value 1” , “value 2”] in the DB, therefore it cant be read and in the CRUD-Controller there is no data saved when i am editing.everything works perfectly except the value i am getting from the data. I would appreciate any help, methods or alternatives

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>

home controller

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

and view:

        <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'));
}

Advertisement

Answer

Firstly you should get selected locations in $selectLocations variable and all locations in $allLocations variable for edit.blade.php

In edit.blade.php, you can display like this:-

@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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement