简体   繁体   中英

how to update data in laravel 5?

My html form in laravel 5.how to display and edit data in laravel form.

 {!!  Form::open(array('url' => 'addstaff','class'=>'form-signin')) !!}

            <div class="row" style="margin-top:-20px;">
                <div class="col-md-12">
                    <h1 class="page-head-line">&nbsp;Staff
                    <div class="col-sm-12" style="font-size:12px">
                            <div class="row" style="margin-top:20px;">
                                <div class="form-group col-sm-2 col-xs-6">
                                <label>Title&nbsp;</label>
                               {!!  Form::select('title', array('1' => 'Select', 'Mr.' => 'Mr.', 'Mrs.' => 'Mrs.','Miss' => 'Miss'), null, ['class' => 'form-control']);  !!}
                                </div>
                                <div class="form-group col-sm-5 col-xs-6">
                                    <label>First Name&nbsp;</label>

                                {!!  Form::text('fname','',array('class' => 'form-control' ,'id' => 'fname' , "placeholder" => 'First Name')) !!}
                                </div>
                                <div class="form-group col-sm-5 col-xs-6">
                                    <label>Second Name&nbsp;</label>
                                    {!!  Form::text('sname','',array('class' => 'form-control' ,'id' => 'fname' , "placeholder" => 'Second Name')) !!}
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>Aadhar </label>
                                    {!!  Form::text('Aadhar','',array('class' => 'form-control' ,'id' => 'Aadhar' , "placeholder" => 'Aadhar Card No.')) !!} 
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>PAN Card </label>
                                     {!!  Form::text('pan','',array('class' => 'form-control' ,'id' => 'Aadhar' , "placeholder" => 'PAN Card No.')) !!}
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>Mobile No.</label>
                                     {!!  Form::text('mobile','',array('class' => 'form-control' ,'id' => 'mobile' , "placeholder" => 'Mobile No.')) !!}
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>Password</label>
                                    {!!  Form::password('Password',array('class' => 'form-control' ,'id' => 'Password' , "placeholder" => 'Password')) !!} 
                                </div>
                                <div class="form-group col-sm-4 col-xs-6">
                                    <label>Address 1</label>
                                     {!!  Form::text('Address1','',array('class' => 'form-control' ,'id' => 'Address1' , "placeholder" => 'Address 1')) !!}
                                </div>
                                <div class="form-group col-sm-4 col-xs-6">
                                    <label>Address 2</label>
                                  {!!  Form::text('Address2','',array('class' => 'form-control' ,'id' => 'Address1' , "placeholder" => 'Address 2')) !!}  

                                </div>
                                <div class="form-group col-sm-4 col-xs-6">
                                    <label>Address 3</label>
                                   {!!  Form::text('Address3','',array('class' => 'form-control' ,'id' => 'Address3' , "placeholder" => 'Landmark')) !!}   

                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>City / Town</label>
                                    {!!  Form::text('city','',array('class' => 'form-control' ,'id' => 'city' , "placeholder" => 'City / Town')) !!}   
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>Pincode</label>
                                   {!!  Form::text('Pincode','',array('class' => 'form-control' ,'id' => 'Pincode' , "placeholder" => 'Pincode')) !!}      
                                </div>
                                <div class="form-group col-sm-3 col-xs-6">
                                    <label>State</label>
                                   {!!  Form::text('State','',array('class' => 'form-control' ,'id' => 'State' , "placeholder" => 'State')) !!}   

                                </div>
                            </div>
                            <div class="text-center">  {!!  Form::submit('Submit', array('class'=>'btn btn-primary btn-md')) !!}</div>
                        </div>
                       <br clear="all">

                        @if(Session::has('successs'))
              <div class="alert text-center alert-success" style="font-size:12px;">
                {!!Session::get('successs')!!} 
                  </div>
           @endif 



           @if(Session::has('error'))
              <div class="alert alert-danger">
                    {!!Session::get('error')!!} 
                     </div>
           @endif  
                    </h1>

                </div>
            </div>

          {!! Form::close() !!}   

Then in edit link I have written the code

<a href="<?php echo 'Editstaff/'.$row->staff_id; ?>" class="btn btn-xs btn-info" >Edit</a>

Then in Route file I have written the code like

Route::get('Editstaff/{id}', 'HomeController@Editstaff');

Then in controller file I have written the code like

public function Editstaff($id)
{

     $editdata = DB::table('staff')->where('staff_id','=',$id)->first(); 
    return View::make('staff',array('list' => $editdata));

}   

I want to update data first show in from when click on edit link ..can some one help me.

First things first: you're working with a form and, in my humble opinion, the id should be placed in a hidden field into your form... Anyway, you can access the form data via the $request variable. Change your method's signature like this:

public function Editstaff(Request $request, $id)

and remember to include the Illuminate\\Http\\Request class with the use statement on the head of your controller (after the namespace, obviously).

Load the record with the given id as follows:

$staff = Staff::find($id);

Consider the use of Staff::findOrFail($id) that (as the method's name tell you) fails if the record for the given id has not been found. The next step consists in setting the data on the fetched model, as you do for the insert:

$staff->myField = $request->input('myField');

Then call the save method of your model

$staff->save()

There's another way for reaching the same goal. Check the Laravel docs for further info on saving and updating models here . You should check the Response chapter too, here

Route

Route::get('Editstaff/{id}', 'HomeController@Editstaff');

Controller

      public function Editstaff(Request $request) 
      {
        $editdata = array(
                  $title = $request->input('title');
        );

        DB::table('staff')->where('id', $id)->update($editdata );
        Session::flash('message', 'Data update successfully');
        return redirect('addstaff');
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM