使用Laravel更新对象

时间:2016-06-09 13:52:55

标签: php laravel eloquent

我想更新“指定”的值,但是当我点击第一项时,表单会自动填充插入的最后一个值,而不是我点击的那个,
例如,如果我想更新“ FM2 ”的值,我会在表格中得到“ tst ”:

Displaying the values

When I click to update "FM2" value

这是视图代码:

    @extends('dashboard.layout')

@section('title' , 'Manage Sockets')
@section('pageHeader', 'Sockets')

@section('content')

    <div class="card">

        <div class="table-responsive">
            <table id="data-table-basic" class="table table-striped">
                <thead>
                <tr>
                    <th data-column-id="id" data-type="numeric">ID</th>
                    <th data-column-id="sender">Designation</th>
                    <th data-column-id="received" data-order="desc">Updated on</th>
                    <th data-column-id="operations" data-order="desc">Operations</th>
                </tr>
                </thead>
                <tbody>
                    @foreach($sockets as $socket)
                        <tr>
                            <td>{{$socket->id}}</td>
                            <td>{{$socket->designation}}</td>
                            <td>{{$socket->updated_at}}</td>
                            <td>
                                <a href="{{action('SocketController@delete', ['id' =>$socket->id ])}}" class="btn bgm-cyan waves-effect"><i class="zmdi zmdi-close"></i></a>
                                <a data-toggle="modal" href="#modalDefault"  class="btn bgm-orange waves-effect"><i class="zmdi zmdi-edit"></i></a>
                            </td>

                        </tr>
                    @endforeach
                </tbody>
            </table>

        </div>

    </div>
    <div class="modal fade" id="modalDefault" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Update A Socket</h4>
                </div>
                <div class="modal-body">
                    {!! Form::model($socket,['route' =>['socketupdate',$socket->id],'method'=>'POST']) !!}
                    <div class="row">
                        <div class=" col-sm-offset-1 col-sm-10" >
                            <!-- text input for designation -->
                            <div class="form-group fg-float">
                                <div class="fg-line">
                                    {!! Form::label('designation', 'designation:',['class' => 'fg-label']) !!}
                                    {!! Form::text('designation',null,['class' => 'form-control fg-input input-sm']) !!}
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row" >
                        <div class="col-sm-12" >
                            <button class="btn bgm-blue center-block" style="display: flex" type="submit" >Submit</button>
                        </div>
                    </div>
                    {!! Form::close() !!}
                </div>
                <div class="modal-footer">
                    <button data-dismiss="modal" class="btn bgm-cyan btn-icon waves-effect waves-circle waves-float"><i class="zmdi zmdi-close"></i></button>
                </div>
            </div>
        </div>
    </div>


    <button class="btn bgm-lightgreen btn-icon waves-effect waves-circle waves-float">
        <i class="zmdi zmdi-plus"></i>
    </button>
    @include('forms.socket')

@endsection

@section('additional_scripts')
    <script type="text/javascript">
        $(document).ready(function(){
            $('body').on('click', '#btn-color-targets > .btn', function(){
                var color = $(this).data('target-color');
                $('#modalColor').attr('data-modal-color', color);
            });
        });
    </script>

@endsection

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在您的代码中,您必须为每条记录单独设置所有模型,这是不可取的 要么 您可以通过jquery点击动态更改引导模式的内容和路由参数ID,否则它只会在您设置它时获取最后输入的记录ID。 所以 变化

GeoJSON

{!! Form::model($socket,['route' =>['socketupdate',$socket->id],'method'=>'POST']) !!}

并在编辑按钮上单击替换rep_id。