Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

littledaggers's avatar

Selected multiple items and saved with json encode aren't displayed as selected in edit, bootstrap selectpicker plugin

Im having an issue with making items selected in a multiple select box with selectpicker javascript plugin appear as selected in the edit page. I am saving the picks as json and have no issue with saving them, but when Im editing the entry all the picks aren't selected and the field is blank.

I tried the following in my edit blade:

  <div class="mb-3 col-md-6">
                    <label for="">Quotation Description:</label>
                    <script>
                        $(function () {
                            $('select').selectpicker();
                        });
                    </script>
                    <select   class="selectpicker form-control" name="joinery_name[]" multiple  required>
                        @php
                        $joinery_name = json_decode($quot_edit->joinery_name)
                        @endphp

                        @foreach($joinery as $joineries)
                             <option value="{{ $joineries->name }}" {{ $quot_edit->joindery_name == $joineries->name ? 'selected':''  }} name="joinery_name[]">{{ $joineries->name }}</option>
                        @endforeach
                    </select>
                </div>

My quot model is

protected $table = 'client_quotation';

    protected $primaryKey = 'quotId';

      protected $fillable = [
            'name',
            'email',
            'phone_number',
            'contact_person',
            'date_of_sending_inquiry',
            'joinery_name',
            'verification_date',
            'notes',
            'verified_receipt',
            'created_by',
            'holding_id'
      ];

      public function followup()
      {
        return $this->hasMany(QuotationFollowup::class, 'client_quotation_id');
      }

my joinery model is

    protected $table = 'joinery';

    protected $fillable = [
          'name',
          'created_by'
    ];
}

What can I change in my edit blade to make the selected values decoded from json appear as selected in the multiple select with selectpicker plugin?

0 likes
5 replies
Tray2's avatar
Tray2
Best Answer
Level 73

I suggest you rethink your strategy, there is almost never a good idea or a valid reason for storing data that you search, or update as json. I suggest you read this post https://tray2.se/posts/database-design

2 likes
littledaggers's avatar

@Tray2 I will give it a read. Thank you.

And in that case how is it possible to store multiple names in the same column? What do you suggest other than json? (if I will figure that out after reading your post, then feel free to let me know so)

Tray2's avatar

@littledaggers You create a separate table for the names and use a foreign key to assign the names to the other table. You will see an example of that in the blog post I linked. The same kind of relation between records and tracks is explained there.

1 like
littledaggers's avatar

@Tray2 I already have a separate table for the joineries and their names. When I was asking the main developer of this project if I should use foreign key relationship, he advised me against it and told me it is not really necessary in this case. I guess he forgot about the U part in CRUD and thought it wasn't gonna be the case because he was really stressed with other projects.

Well I guess the moral of this story is that to never ask someone about something while they are under pressure haha.

Since I have already seen the foreign key relationship work for me yesterday with your help, I will just go for it and update the post when I get it to work. For now I will continue reading your post you suggested.

Thank you so much for your time!

1 like
Tray2's avatar

@littledaggers It two tables are references each other in any way they should have some kind of foreign key.

Relations are the whole point of a relational database.

1 like

Please or to participate in this conversation.