Laravel-没有有关模型App / User的查询结果

时间:2018-12-04 11:17:55

标签: javascript php jquery html laravel

没有关于模型[App \ User]的查询结果。我找不到它向我显示的原因...我想将用户角色提交到数据库,当我选择角色并按Submit时,会弹出此错误。以前,我每行都有一个提交按钮,我只需要一个提交按钮。我不知道为什么会弹出此错误。请帮助

控制器

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));
        }
    }

    /**
     * GenerateUserTable metoda očitavanja korisnika u tablici na admin stranici
     *
     * @param  $data
     *
     * @return array
     */
    public function generateUserTable($data)
    {
        // return User::find(2)->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 = '';
                    if (in_array($roles1->id, $userRoles, true)) {
                        $checked = 'checked="checked"';
                    }
                    $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role['.$roles1->id.'][]" value="'.$roles1->id.'" class="checkbox'.$roles1->id.'" id="'.$roles1->id.'">'.' ' : '';
                }
                $output .= '
                    <form method="post">    
                        <tr input type="hidden" name="user" value="'.$row->id.'">
                            <td>'.$row->surname.'</td>
                            <td>'.$row->name.'</td>
                            <td>'.$row->phone.'</td>
                            <td>'.$roleNames.'</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>
                    </form>
                ';
            }
        } else {
            $output = '
                <tr>
                    <td align="center" colspan="5">Nema podataka</td>
                </tr>
            ';
        }
        return array(
            'table_data'  => $output,
            'total_data'  => $total_row,
        );
    }

使用脚本查看

<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>
                    <button type="submit" id="potvrdi" class="potvrdi-button btn btn-primary">
                        <div class="potvrdi">Potvrdi</div>
                    </button>
                    <button id="ispisi" class="ispisi-button btn btn-primary">
                        <div class="ispisi">Ispiši</div>
                    </button>
            </div>
        </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);
                }
            })
        })

        $(document).on('keyup', '#search', function(){
            var query = $(this).val();
            fetch_user_data(query);
        });

        $('#users').on('click', '.remove-button', function(){
            var id = $(this).data('id');
            $(".test").attr('data-id', id);
            $("#deleteModal").modal("show");
        });

        $(document).on('click', '.bck-mod', function(){
            var id = $(this).data('id');
            $.ajax({
                //url:"{{ route('live_search.destroy') }}",
                url:"/live_search/destroy",
                method:"get",
                data:{id:id},
                dataType:'json',
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                },
                error: function(data)
                {
                    console.log(data);
                }
            })
        });
    });
</script>

0 个答案:

没有答案