Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

Select2 init data


i have select 2 in create page
how i can load with option user selected from db (save from create page) to edit/show page
i'm using laravel
thanks


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi,

Sorry for the delay. Select2 with multiple select, you can add multiple="multiple" attribute to the select input. And the name


<select name="selectedOptions[]" multiple="multiple">


For more information, please check the select2 docs.
https://select2.org/getting-started/basic-usage#multi-select-boxes-pillbox

Thanks



Hi,


Assuming you have stored the selected options in your database during the creation process, retrieve these options for the specific record you are editing. You might have a model representing the data, and you can use that model to retrieve the data from the database.

In your controller, retrieve the data and pass it to the view where you want to show the edit form.


// In your controller method
public function edit($id)
{
// Replace "YourModel" with your actual model name
$record = YourModel::find($id);

return view("your.edit.view", compact("record"));
}


In your view file (blade file), use the passed data to initialize Select2 with the selected options.


<!-- Your form code -->
<form action="{{ route("your.update.route", $record->id) }}" method="POST">
@csrf
@method("PUT")

<!-- Other form fields -->

<select name="your_select_field" class="form-control">
<!-- Populate options from database -->
@foreach ($optionsFromDatabase as $option)
<option value="{{ $option->id }}" {{ $record->selectedOption == $option->id ? "selected" : "" }}>
{{ $option->name }}
</option>
@endforeach
</select>

<!-- Other form fields -->

<button type="submit">Update</button>
</form>


In this example, optionsFromDatabase represents the options you have in your database, and selectedOption is the property from your model that holds the selected option for the record being edited.

Make sure to replace YourModel with the actual name of your model, and adjust the field names accordingly based on your database schema.



hi Faizal
sorry for lack of information
but i mean select2 multiple select


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(