Laravel 5在更新时不会更新

时间:2018-04-04 08:35:27

标签: php database forms laravel-5

我开始学习Laravel 5.6并且我通过基本(MVC,路线等)制作它,但我是完全初学者。 我开始用laravel创建CRUD。我做了创建,阅读但我停止更新,我遇到了一个错误。当我选择一个用户(korisnik)时,它会打开我的edite.blade.php视图,表单中会填充用户数据。但是当我更改数据时,我提交了编辑表单,它不会更改db中的用户,但是我获得了成功的flash会话消息

我的编辑表单:



{!! Form::model($user, ['route'=>['user.update', $user->Id], 'method'=>'PUT']) !!}
        <div class="col-md-6">

            {{Form::label('First_name', 'First name:')}}<br><br>
            {{ Form::text('First_name', null, ["class"=> 'form-control']) }}<br><br>

            {{Form::label('Last_name', 'Last name:')}}<br><br>
            {{ Form::text('Last_name', null, ["class"=> 'form-control']) }}<br><br>

            {{Form::label('Email', 'Email:')}}<br><br>
            {{ Form::text('Email'), null, ["class"=> 'form-control'] }}<br><br>

            {!!Form::label('Password', 'Password:')!!}<br><br>
            {!!Form::password('Password', null, array('class' => 'form-control', 'required' => ''))!!}<br><br>

            {{Form::label('Lokacija', 'Lokacija:')}}<br><br>
            {{ Form::text('Lokacija'), null, ["class"=> 'form-control'] }}
           


        </div>
        <div class="col-md-6">
            <div class="well">
                <dl class="dl-horizontal">

                    <dt>Korisnik kreiran:</dt>
                    <dd>{{ date( 'j M, Y H:i', strtotime($user->created_at)) }}</dd>
                </dl>
                <dl class="dl-horizontal">

                    <dt>Korisnik ažuriran:</dt>
                    <dd>{{ date( 'j M, Y H:i', strtotime($user->updated_at)) }}</dd>
                </dl>
                <hr>
                <div class="row">
                    <div class="col-md-6">
                        {!! Html::linkRoute('user.show', 'Cancle', array($user->Id), ['class' => 'btn btn-danger btn-block']) !!}

                    </div>
                    <div class="col-md-6">

                        {{Form::submit('Save', ['class'=>'btn btn-success btn-block'])}}

                    </div>
                </div>

            </div>
        </div>
        {!! Form::close() !!}
&#13;
&#13;
&#13;

我的控制器:

&#13;
&#13;
public function edit($id)
    {
       
        //find the user from db and save it as var

        $user= User::find($id);


        //return a view and pass in the var we prev created

        return view('user.edit')->with('user', $user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //Validate the data
        $this->validate($request, array(

            'First_name'=>'required|max:100',
            'Last_name'=>'required|max:100',
            'Email'=>'required',
            'Password'=>'required',
            'Lokacija'=>'required'
        ));

        //Save the data to db
        $user = User::find($id);

        $user->First_name = $request->input('First_name');
        $user->Last_name = $request->input('Last_name');
        $user->Email = $request->input('Email');
        $user->Password = $request->input('Password');
        $user->Lokacija = $request->input('Lokacija');

        $user->save();

        //set flash data with success message
        Session::flash('success', ' Korisnik uspješno ažuriran!');

        //redirect with flash data to users.show
        return redirect()->route('user.show', $user->id);
    }
&#13;
&#13;
&#13;

我希望我解释得很好,英语不太好。

0 个答案:

没有答案