mercredi 4 avril 2018

Laravel 5 when updating it doesn't update

I tarted learning Laravel 5.6 and I made it thru basic (MVC, route ect.), but i am complete beginner. I started to create CRUD with laravel. I made create, read but i stoped at updating, i am experiencing a bug. When i chose a user(korisnik) it opens my edite.blade.php view and the form is populated with users data. But when i change the data i submit the edit form it doesn't change the user in db, but i get the flash session message of success

My edit form:

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

            <br><br>
            <br><br>

            <br><br>
            <br><br>

            <br><br>
            <br><br>

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

            <br><br>
            
           


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

                    <dt>Korisnik kreiran:</dt>
                    <dd></dd>
                </dl>
                <dl class="dl-horizontal">

                    <dt>Korisnik ažuriran:</dt>
                    <dd></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">

                        

                    </div>
                </div>

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

My controller:

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

I hope i explained it well, sry not so good in English.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire