0

I am trying to show all selected values from database to multiple select2.So when I want to add other events I can add them.

Offer tables:
id:1
title: event1
events: 17,6,8

column events has id of all events selected.

This is what I do:

public function edit($id)
    {
        $offer=Offers::find($id);
         $events=Events::all();

        $explode=  explode(',', $offer->events);


        return view('edit',['offer'=>$offer,'events'=>$events,'explode'=>$explode]);
    }

Blade:

<div class="form-group m-form__group row">
    <label class="col-xl-3 col-lg-3 col-form-label">****</label>
    <div class="col-xl-9 col-lg-9">
        <select class="form-control m-select2" id="m_select2_3" name="events[]" multiple="multiple">
            <optgroup label="Events">
                @foreach($events as $event)
                    @foreach($explode as $item)
                        <option value="{{$event->id}}"  {{ $item == $event->id ? 'selected="selected"' : ''}}>{{$event->title}}</option>
                    @endforeach
                @endforeach
            </optgroup>
        </select>
    </div>
</div>

So when i select the value it shows three times repeated values

enter image description here

4
  • can you post your controller code aswell please? Commented Feb 4, 2019 at 14:41
  • is offer to events one-to-many relations? You're getting duplicates from your foreach, there are multiple events that belongs to an offer. Commented Feb 4, 2019 at 14:50
  • 2
    remove the @foreach $explode, use in_array($event->id,$explode); Commented Feb 4, 2019 at 14:54
  • @davidloper thank you Commented Feb 4, 2019 at 14:56

2 Answers 2

1

You're loading all the database events in the $events variable and not the events associated with the offer. You should be doing something like this:

$events = Event::find(explode(',', Offer::find($id)->events));

Having said that, it would be best to use relationships for that instead of manually setting the ids https://laravel.com/docs/5.7/eloquent-relationships

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe when you are getting events, you are getting duplicated, try this:

public function edit($id)
    {
        $offer=Offers::find($id);
         $events=Events::all();

        $explode =  array_values(array_unique(explode(',', $offer->events)));



        return view('edit',['offer'=>$offer,'events'=>$events,'explode'=>$explode]);
    }

However, if that's the case, you should know that you offers table do not have duplicated event at the first place so you might want to change the script which saves events as well for the offer to remove duplicate entries.

Also, I don't know if your "events" table contains duplicated events as well. Please check that and remove any duplicated values from your "events" table and if you cant then use same code for that as well like so in your controller:

$events = array_values(array_unique(Events::all()->toArray()));

I hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.