dimanche 14 février 2016

Update multiple data from in database using laravel 5.1

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire