如何从Laravel 5.2中的表单更新数据

时间:2016-09-04 16:37:08

标签: php laravel

您好我在编辑和更新表单中的数据方面遇到了问题。我已经尝试从表单中获取数据,但是当我尝试编辑和更新它时,它将生成另一个数据。谢谢!

Controller: 
        public function registerPackage()
    {       
        $activity_packages = Chap_activity_packages::all();
        return view('admin.registerPackage',compact('activity_packages'));
    }

    public function savePackage(Request $request)

    {
        $this->validate($request,[
        'chap_activity_packages_name'=>'required|Min:4|unique:chap_activity_packa        ges',
        'chap_activity_packages_price'=>'required|numeric'
        ]);

        $values = $request->all();
        Chap_activity_packages::create($values);
        return view('admin.registerPackage');
    }

    public function editPackage($id)
    { 
        $act = Chap_activity_packages::find($id);
        $activity_packages=Chap_activity_packages::all();

        return view('admin.registerPackage',compact('act','activity_packages'));

    }

         <h4 class="page-header">Packages Management</h4>
                <div class="row">
                    <div class="col-xs-5">
                        <div class="panel panel-success">
                            <div class="panel-heading">
                                <h3 class="panel-title">Register New Package</h3>

                                        @if($errors->any())
                                                <div class="alert alert-danger">
                                                    @foreach($errors->all() as $error)
                                                        <div>{{ $error }}</div>
                                                    @endforeach
                                                </div>
                                        @endif

                                        @if(Session::has('flash_message'))
                                                <div class="alert alert-success">
                                                      {{ Session::get('flash_message') }}
                                                </div>
                                        @endif


                            </div>
                                <div class="panel-body">
                                {{ Form::open(array('url' => 'admin/registerPackage')) }}

                                <div class="form-group">
                            {{ Form::label('chap_name','Package Name:') }}
                            {{ Form::text('chap_activity_packages_name',isset($act)? $act->chap_activity_packages_name:null,['class' => 'form-control', 'placeholder' => 'Enter package name']) }}
                                </div>          

                                    <div class="form-group">
                            {{ Form::label('chap_price','Package Price:') }}
                            {{ Form::text('chap_activity_packages_price',isset($act)? $act->chap_activity_packages_price:null,['class' => 'form-control', 'placeholder' => 'Enter package price']) }}
                                    </div>

                                    <div class="form-group">
                                        {{ Form::submit('Register Package',['class' => 'btn btn-success btn-block']) }}
                                    </div>

                                {{ Form::close() }}
                            </div>
                        </div>
                    </div>
                </div>


@stop
@section('content2')
                                    <div class="panel panel-success">
                                        <div class="panel-heading">
                                            <h4 class="panel-title">Available Packages</h4>
                                        </div>
                                        <div class="panel-body">
                                            <table class="table table-striped table-hover">
                                                <thead>
                                                    <tr>
                                                        <th>Package Name</th>
                                                        <th>Package Price</th>
                                                        <th>Action</th>
                                                    </tr>
                                                </thead>
                                                <tbody>

                                                            @foreach($activity_packages as $activity_package)

                                                                    <tr>
                                                                        <td> {{ $activity_package->chap_activity_packages_name }} </td>
                                                                        <td> {{ $activity_package->chap_activity_packages_price }} </td>

                                                                        <td>

                                                                                <a href='{{ url("admin/editPackage/$activity_package->id") }}'><span class="glyphicon glyphicon-pencil"></span></a>

                                                                                <a href="#"><span class="glyphicon glyphicon-trash"></span></a>
                                                                        </td>

                                                                    </tr>

                                                            @endforeach

                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
      @stop

这些是控制器和视图的代码。如果出了什么问题,请告诉我。非常感谢!

1 个答案:

答案 0 :(得分:0)

我没有看到更新控制器中数据的方法,它应该如下所示:

// YourController.php, use implicit route model binding
public function update(App\Chap_activity_packages $model)
{
    // do your validation here
    // $this->validate(blahblahblah...)
    // ...
    $model->update(request()->all());
    return redirect()->back()->with('msg', 'Update success.'); 
}

// routes.php
Route::put('your/route/to/model/{model}', 'YourController@update');

// yourTemplate.blade.php
// remember to use method spoofing
{{ Form::open(['url' => 'your/route/to/model/' . $model->id, 'method' => 'put']) }} 

可以找到有关路线模型绑定的信息here。祝你好运!