Latest

Tuesday, July 11, 2017

Get selected values from database in checkboxes (Laravel)

Asked by: Nathan Siafa


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:

enter image description here

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

enter image description here

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?


Answers

Answered by: Arigi Wiratama at 2017-07-11 07:44PM



I think I can help you. First, The Route:

Route::get('subject/{id}/edit', 'YourController@edit');

And then in Subject model, make sure you set the relations between subject and the pivot table. So you can get the division_id with accessing the Subject model.

public function pivots() {

    // change this PivotTable with correct Class

    return $this->hasMany(PivotTable::class)
}

And then let's move to edit method in yourController, you have to make a new array like this

public function edit($id) {

    // first we get all data from subjects and divisions table 
    // get the Subject model first, set it in $subject var
    // get all 'division_id' from the pivot table and set it in $selectedDiv 
    // and get all divison from divisons table, and set it in $currDivisions

    $subject = Subject::find($id);
    $selectedDiv = $subject->pivots()->pluck('division_id');
    $currDivisions =  Divison::get();

    // Loop the $currDivisions to make new Array called $divisions
    foreach ($currDivisions as $k) {
        $divisions[$k->id] = $k->name; 
    }
    // it will make the $divisions variable to an array like this 
    // $divisions = [1 => 'Elementary', 2 => 'Junior HS']
    // pass all variables in your view 

}


And in your view I think we can use ternary if there, and for the usage I use array_key_exists method to the condition:

@foreach($divisions as $divisionKey => $division)

    <label class="checkbox-inline">
        <input type="checkbox" id="division_id" name="division_id[]" {{array_key_exists($divisionKey, $selectedDiv) ? 'checked=""' :  ''}} value="{{$divisionKey}}"> {{$division}}
    </label>

@endforeach

And I think it should be make the checkbox checked :-D




Source

No comments:

Post a Comment

Adbox