I'm working on a laravel application. In this application I have three tables:
subjects
id | name
-- | ----
01 | maths
02 | english
divisions
id | name
-- | ----
01 | Senior High
02 | Junior High
division_subject pivot table
division_id | subject_id
--- | ---
01 | maths
02 | english
A subject can have many divisions, so, in my form I have a list of all divisions as checkboxes. If asubject is taught in a single division a single division will be checked. If the subject is taught is many divisons all the divisions the subject is taught in will be checked also. Now this is working fine when inserting with no problems.
This is how the form looks to insert records:

After records are inserted this is how they are displayed in the table:

Html code for displaying table:
<tbody id="subjects">
@foreach($subjects as $subject)
<tr>
<td data-type="text" data-name="name" class="name" data-pk="{{$subject->id}}">{{$subject->name}}</td>
<td>
<!-- If a subject belongs to a division or divisions list all the divisions
that belongs to the subject -->
@if(count($subject->divisions))
@foreach($subject->divisions as $division)
<a href="#" data-type="checklist" data-value="{{$division->id}}" data-title="Select divisions" data-name="division" class="division" data-pk="{{$division->id}}" role="button">
<span class="badge label-primary">{{$division->name}}</span>
</a>
@endforeach
@endif
</td>
<td>
<a class="edit-modal" data-id="{{$subject->id}}" data-name="{{$subject->name}}" data-toggle="tooltip" title="Edit" href="#" role="button">
<i class="glyphicon glyphicon-edit text-info"></i>
</a>
</td>
<td>
<a id="delete" data-id="{{$subject->id}}" data-toggle="tooltip" title="Delete" href="#" role="button">
<i class="glyphicon glyphicon-trash text-danger"></i>
</a>
</td>
</tr>
@endforeach
Now I want when I click on the edit button a bootstrap dialog will pop
out and show details for the record to be edited. If the subject has divisions assigned or a division assigned, than all the division assigned to the subject and stored in the pivot table should be checked. And those divisions that are not assigned unchecked.
To list a the subject in the form I'm using a view composer in the App service provider to pass the division data to my view:
view()->composer('subjects.show', function ($view){
$view->with('divisions', \App\Division::all());
});
Now in my form this is how I'm listing the divisions as seen in the picture above:
<div class="form-group">
<label for="division">Level</label>
<p class="text-muted">Please select the division(s) the subject is taught within.</p>
@foreach($divisions as $division)
<label class="checkbox-inline">
<input type="checkbox" id="division_id" name="division_id[]" value="{{$division->id}}"> {{$division->name}}
</label>
@endforeach
</div>
How can I refactor this to checked all values that are assigned to a subject and have those values which are not assign unchecked?