将角色按钮提交到数据透视表

时间:2018-11-22 08:41:50

标签: php jquery laravel

我有2个模型,用户,角色和数据透视表。在数据透视表中可见对用户的所有指定角色(user_id | role_id)当前,在控制器中,每行都有自己的提交按钮,用于将角色提交给数据库。我只做了一个按钮,但是它不起作用,它抛出异常“模型[App \ User]没有查询结果”。另外,我正在使用ajax,但是您将在下面的代码中看到所有内容。

我将其分开,因为generateUserTable也连接到destroy和action方法。按钮在generateUserTable中-

<td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                    <div class="potvrdi">Potvrdi</div>
</button></td>

控制器-

public function postUserRole(Request $request)
    {
        if ($request->ajax()) {
            $loged_in_user = User::findOrFail(Auth::user()->id);
            $user = User::findOrFail($request->get('id'));
            $r1 = Role::find(1);
            $r2 = Role::find(2);
            if ($loged_in_user->IsAdmin()) {
                if ($request->get('admin_role')==1) {
                    $user->roles()->attach($r1);
                } else {
                    $user->roles()->detach($r1);
                }
                if ($request->get('korisnik_role')==1) {
                    $user->roles()->attach($r2);
                } else {
                    $user->roles()->detach($r2);
                }
            }
            $data = User::all();
            return json_encode($this->generateUserTable($data));
        }
    }

public function generateUserTable($data)
{
    // return User::find(5)->roles;
    $total_row = $data->count();
    $output = "";
    if ($total_row > 0) {
        foreach ($data as $row) {
            $roleNames = '';
            $userRoles = $row->roles()->pluck('id')->toArray();
            foreach (Role::all() as $roles1) {
                $checked = '';
            // var_dump($userRoles);
            // var_dump($roles1->id);
                    // var_dump($roles1);
                if (in_array($roles1->id, $userRoles, true)) {
                    $checked = 'checked="checked"';
                }
                $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox'.$roles1->id.'" id="'.$roles1->id.'">'.' ' : '';
            }
            $output .= '
                <tr>
                    <td>'.$row->surname.'</td>
                    <td>'.$row->name.'</td>
                    <td>'.$row->phone.'</td>
                    <td>'.$roleNames.'</td>
                    <td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                    <div class="potvrdi">Potvrdi</div>
                    </button></td>
                    <td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
                    <div class="close">&#120;</div>
                    </button></td>
                </tr>
            ';
        }
    } else {
        $output = '
            <tr>
                <td align="center" colspan="5">Nema podataka</td>
            </tr>
        ';
    }
    return array(
        'table_data'  => $output,
        'total_data'  => $total_row,
    );
}

}

查看-这是我希望“转移”提交按钮的地方。我把它放在桌子后面。正在查看脚本,用于向数据库提交用户角色的ajax是第二个。

<div class="container">
    <div class="panel panel-default">
    <div class="panel-heading">Pretraži korisnike</div>
        <div class="panel-body">
            <div class="form-group">
                <input type="text" name="search" id="search" class="form-control" placeholder="Pretraži korisnike" />
            </div>
            <div class="table-responsive">
                <h3 align="center">Broj korisnika: <span id="total_records"></span></h3>
                <table id="users" class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Prezime</th>
                            <th>Ime</th>
                            <th>Telefon</th>
                            <th>Rola</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                <tbody>

                </tbody>
                </table>
            </div>            
            <button type="button" id="potvrdi" class="potvrdi-button btn btn-primary">
                <div class="potvrdi">Potvrdi</div>
            </button>
        </div>    
    </div>
</div>


<script>   
    $(document).ready(function(){
        fetch_user_data();
        function fetch_user_data(query = ''){
            $.ajax({
                url:"{{ route('live_search.action') }}",
                method:'GET',
                data:{query:query},
                dataType:'json',
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
        }

        $(document).on('click', '.potvrdi-button', function(){
            var id = $(this).data('id');
            var admin_role = $(this).parent().parent().find('td:nth-child(4) .checkbox1:checkbox:checked').length;
            console.log(admin_role);
            var korisnik_role = $(this).parent().parent().find('td:nth-child(4) .checkbox2:checkbox:checked').length;
            console.log(korisnik_role);
            $.ajax({
                url: "{{ route('live_search.postUserRole') }}",
                method: "post",
                data: {
                    id:id,
                    admin_role: admin_role,
                    korisnik_role: korisnik_role,
                    _token:'{{ csrf_token() }}'
                    },
                dataType: 'json',
                success: function(data) {
                    $('.checkbox1').prop('checked', false);
                    $('.checkbox2').prop('checked', false);
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
        })

编辑路线

Route::group(['middleware' => ['web', 'auth']], function () {
    Route::get('/admin', 'PagesController@admin');
    Route::post('/adminForma', 'PagesController@adminForma')->name('adminForma');
    Route::get('/users', 'PagesController@users');
    Route::get('/settings', 'PagesController@settings');
    Route::post('/settings', 'PagesController@settings');
    Route::get('/generateUserTable', 'PagesController@generateUserTable');
    Route::post('/generateUserTable', 'PagesController@generateUserTable');
    Route::get('/live_search/action', 'PagesController@action')->name('live_search.action');
    Route::post('/live_search/action', 'PagesController@action');
    Route::get('/live_search/postUserRole', 'PagesController@postUserRole')->name('live_search.postUserRole');
    Route::post('/live_search/postUserRole', 'PagesController@postUserRole');
    Route::get('/live_search/destroy', 'PagesController@destroy')->name('live_search.destroy');
    Route::post('/live_search/generateUserTable', 'PagesController@generateUserTable');
    Route::get('/live_search/generateUserTable', 'PagesController@generateUserTable')->name('live_search.generateUserTable');
});

我仍然是初学者,因此我对意大利面条代码表示歉意,我正在尽力而为。

谢谢

0 个答案:

没有答案