1
votes

i have some coding, i wanna to update multiple data from select option value in database using laravel 5.1. i'm using ajax onchange="form.submit()", so i can update data without submit. this is my view

{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
                        <table class="dataTable" id="table-user">
                            <thead class="grey lighten-3">
                                <tr>
                                    <td class="center-align no-sort">Photo</td>
                                    <td class="center-align">Fullname</td>
                                    <td class="center-align">Rule</td>
                                    <td class="center-align">Action</td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $hitung = $UserAccess->count(); ?>
                                @foreach($UserAccess as $list)
                                <tr>
                                    <td class="center-align" width="50">
                                        @if($list->avatar == NULL)
                                        <img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
                                        @else
                                        <img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
                                        @endif
                                    </td>
                                    <td class="red1-text lato-bold center-align">{{$list->name}}</td>
                                    <td class="center-align" width="150">
                                        <select id="selectrole" name="selectrole" onchange="form.submit()">
                                            @foreach($UserAccessRole as $listRole)
                                                <option value="{{$listRole->id}}" @if($list->user_access_role_id_fk == $listRole->id) selected="selected"@endif>{{$listRole->name}}</option>
                                            @endforeach
                                        </select>
                                    </td>
                                    <input type = "hidden" value = "{{$list->id}}" name = "idmain">
                                    @if($hitung > 2)
                                        <td class="center-align">
                                            <a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
                                        </td>
                                    @else
                                        <td class="center-align">Remove Access</td>
                                    @endif
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {!! Form::close() !!}

this is my controller

public function doUpdateAccessRole(Request $request)
  {
    $UserAccess = UserAccess::orderBy('name', 'asc')->get();
    $id_main = $request->input('idmain');
    $id_role = $request->input('selectrole');
    $Role = UserAccess::findOrFail($id_main);
    $Role->user_access_role_id_fk = $id_role;
    $Role->update($request->all());
    return redirect('setting/useraccess');
  }

this is my route

Route::post('setting/updaterole',['uses'=>'SettingController@doUpdateAccessRole','as'=>'updateaccessrole']);

using my coding update function already work, but just last id has been update. please help me, thank you

1
please explain it little more, for which thing last ID updated? and are you trying to update multiple roles for single user ? $Role->update($request->all()); will work for single record. you have to update multiple records, you need sync() method - Qazi
@Qazi tq for your comment. i mean last id update is, for example i have 3 data. the first data have id : 1, data two have id : 2, and the last data have id : 3. if i update using my program, just data with id 3 has been update. please explain to me, what meaning i need sync() method, thank you. - Hartolio Wijaya
eg: user has many roles (1=>student, 2=>author, 12=>instructor) all Selected from multi select dropdown, so at PHP end, you will get these like this roles=>[1=>1, 2=>2, 3=>12] So, you have to add all roles with users, Follow this '$user->roles()->sync($roles);' look here - Qazi
@Qazi I still didn't get it. i don't understand how to use sync() - Hartolio Wijaya
do you have relationship created for user roles ? actually you didn't provided your DB structure for User and roles, thats why cannot guess it properly. try this $Role->sync($id_role); instead of your this code $Role->update($request->all()); - Qazi

1 Answers

1
votes

my code already work. just add foreach in my controller. like this, my controller :

public function doUpdateAccessRole(Request $request)
{

$UserAccess = UserAccess::all();
foreach($UserAccess as $list)
{
  $id_main = $request->input('idmain'.$list->id);
  $id_role = $request->input('selectrole'.$list->id);
  $Role = UserAccess::find($id_main);
  $Role->user_access_role_id_fk = $id_role;
  $Role->update($request->all());
}
  return redirect('setting/useraccess');
}

and this my view :

{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
                        <table class="dataTable" id="table-user">
                            <thead class="grey lighten-3">
                                <tr>
                                    <td class="center-align no-sort">Photo</td>
                                    <td class="center-align">Fullname</td>
                                    <td class="center-align">Rule</td>
                                    <td class="center-align">Action</td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $hitung = $UserAccess->count(); ?>
                                @foreach($UserAccess as $list)
                                <tr>
                                    <td class="center-align" width="50">
                                        @if($list->avatar == NULL)
                                        <img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
                                        @else
                                        <img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
                                        @endif
                                    </td>
                                    <td class="red1-text lato-bold center-align">{{$list->name}}</td>
                                    <td class="center-align" width="150">
                                        <select id="selectrole" name="selectrole{{$list->id}}" onchange="form.submit()">
                                            @foreach($UserAccessRole as $listRole)
                                                <option value="{{$listRole->id}}" @if($list->user_access_role_id_fk == $listRole->id) selected="selected"@endif>{{$listRole->name}}</option>
                                            @endforeach
                                        </select>
                                    </td>
                                    <input type = "hidden" value = "{{$list->id}}" name = "idmain{{$list->id}}">
                                    @if($hitung > 2)
                                        <td class="center-align">
                                            <a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
                                        </td>
                                    @else
                                        <td class="center-align">Remove Access</td>
                                    @endif
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
{!! Form::close() !!}

and now my coding already work.