How to get selected categories on edit record with Select2
How to get selected categories on edit record with Select2
We’ll attempt to use programming in this lesson to solve the "How to get selected categories on edit record with Select2" puzzle.
You can get the selected categories on edit record with select2 in select box. You have to get all the categories which is related to current record and use json_encode() method to convert it into json format and pass it to map method to get only the id attribute and pass to select input using val() method.-
Get selected categories on edit record using Select2
//jQuery function to auto select select2 box on edit record <script> $(document).ready(function(){ var catObj = <?php echo json_encode($post->categories); ?>; var arr = $.map(catObj, function(el) { return el['id']; }); //pass array object value to select2 $('#categoryPost').val(arr).trigger('change'); $('#categoryPost').select2(); }) </script> //Select2 HTML element <select class="js-example-basic-multiple form-control" id="categoryPost" name="category_id[]" multiple="multiple"> <option value="">Select Categories</option> @foreach ($categories as $category) <option value="{{ $category->id }}" {{ $category->id === old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option> @if ($category->children) @foreach ($category->children as $child) <option value="{{ $child->id }}" {{ $child->id === old('category_id') ? 'selected' : '' }}> {{ $child->name }}</option> @endforeach @endif @endforeach </select> <link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
You have to return all the categories in $categories , and post in $post which you want to edit to view file. You have to create a pivot table category_post to store the post_id and category_id and create a categories() method with belongsToMany() in the post model. Don't forget to create categories and posts table. Follow the instruction it will help you to find out the solution for auto select on edit record using select2 with multiple categories.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to fill a column automatically while creating records in Laravel
- On delete set foreign id column value null using migration in laravel 8
- How to Get records between two dates in Laravel
- How to update record after save method in Laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Remove several global scope from query
- How to get specific columns using Laravel eloquent methods
- How to return error message from controller to view in laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to upload image in laravel 8
- How to get column names from table in Laravel
- How to get date from created_at field in laravel
- Sample .htaccess file and index.php file under public directory in laravel
- Get comma separated email from input array
- How to check record exist or not in relationship table
- Insert Comma Separated Values in laravel
- How to upload multiple images after preview in laravel using cropper js
- Generate random string lowercase in Laravel
- Extract only time from datetime in laravel
- How to add class to tr in table using foreach in laravel
- Conditional where clause in Laravel
- Automatically remove records using Prunable trait in Laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Post model with title and body in laravel 8
- How to restore multiple records after soft-deletes in Laravel