title: How to update a database table in laravel 5.2
body: <p>i created one project in laravel 5.2. In that "myform.blade.php" page , created form for register the user. after registration it will show the current user in table format("index.blade.php"). There i given two dynamic button like the drop inside the table. One for Edit and other for Edit/View. When i click on delete button it will delete the corresponding row from the database by taken Primary key(employeeID) as reference id, it's working properly. if i click on Edit/View button it will redirect to "edit.blade.php". There i created same form as in myform.blade.php. If we want to edit the details we can edit from there. I can able to fetch the data from database to the form that i created in the "edit.blade.php". But i don't know how to update the data from their without inserting the same data again(It is not possible, because it will create Integrity constraint violation by trying to insert duplicate primary key. Can any one please tell me how to do updation. Replies are appreciable.</p>
  
  <p>"myform.blade.php" is</p>
  
  <pre><code>  @extends('app')
  
  @section('content')
  
      &lt;div class="templatemo-content-wrapper"&gt;
          &lt;div class="container"&gt;
              &lt;ol class="breadcrumb"&gt;
                  &lt;li&gt;&lt;a href="{{ url("/") }}"&gt;&lt;font color="green"&gt;Home&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                  &lt;li class="active"&gt;Employee Form&lt;/li&gt;
              &lt;/ol&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col-md-8 col-md-offset-2"&gt;
                      &lt;div class="panel panel-success"&gt;
                          &lt;div class="panel-heading"&gt;Employee Form&lt;/div&gt;
                          &lt;div class="panel-body"&gt;
                              @if (count($errors) &gt; 0)
                                  &lt;div class="alert alert-danger"&gt;
                                      &lt;strong&gt;Whoops!&lt;/strong&gt; There were some problems with your input.&lt;br&gt;&lt;br&gt;
                                      &lt;ul&gt;
                                          @foreach ($errors-&gt;all() as $error)
                                              &lt;li&gt;{{ $error }}&lt;/li&gt;
                                          @endforeach
                                      &lt;/ul&gt;
                                  &lt;/div&gt;
                              @endif
  
                              &lt;form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}"&gt;
                                  &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Employee ID&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;E_number&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="employeeNo" value="{{ old('employeeNo') }}" placeholder="Enter employee number"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Name&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="Cname" value="{{ old('Cname') }}" placeholder="Enter Contact Name"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;DOB&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="date" class="form-control" name="dob" value="{{ old('dob') }}" placeholder="Enter date of birth"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Contact Phone&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="phoneNumber" value="{{ old('phoneNumber') }}" placeholder="Enter Mobile Number"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
  
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Address&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="address" value="{{ old('address') }}" placeholder="Enter Address"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;div class="col-md-6 col-md-offset-4"&gt;
                                          &lt;button type="submit" class="btn btn-warning"&gt;
                                              Save
                                          &lt;/button&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;a href="{{ url('myform/index') }}"&gt;view Data&lt;/a&gt;
  
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
  @endsection
  </code></pre>
  
  <p>"index.blade.php" is</p>
  
  <pre><code> @extends('app')
  
  @section('content')
  
  
      &lt;div class="templatemo-content-wrapper" xmlns="http://www.w3.org/1999/html"&gt;
          &lt;ol class="breadcrumb"&gt;
              &lt;li&gt;&lt;a href="{{ url("/") }}"&gt;&lt;font color="green"&gt;Home&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
              &lt;li class="active"&gt;user information&lt;/li&gt;
          &lt;/ol&gt;
          &lt;div class="templatemo-content"&gt;
  
              &lt;h1&gt;View/Edit user information&lt;/h1&gt;
  
              &lt;div&gt;
                  &lt;div&gt;
                      &lt;div&gt;
  
                          &lt;table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc"&gt;
                              &lt;thead&gt;
                              &lt;tr&gt;
                                  &lt;th&gt;Employee ID&lt;/th&gt;
                                  &lt;th&gt;Employee No&lt;/th&gt;
                                  &lt;th&gt;Contact Name&lt;/th&gt;
                                  &lt;th&gt;Date of birth&lt;/th&gt;
                                  &lt;th&gt;Mobile number&lt;/th&gt;
                                  &lt;th&gt;address&lt;/th&gt;
  
                              &lt;/tr&gt;
                              &lt;/thead&gt;
                              &lt;tbody&gt;
  
                              {{--{{ UserController::getIndex() }}--}}
                              @foreach($employer as $emp)
                                  &lt;tr&gt;
                                      &lt;td&gt;{{ $emp-&gt;employeeID }}&lt;/td&gt;
                                      &lt;td&gt;{{ $emp-&gt;employeeNo }}&lt;/td&gt;
                                      &lt;td&gt;{{ $emp-&gt;Cname }}&lt;/td&gt;
                                      &lt;td&gt;{{ $emp-&gt;dob }}&lt;/td&gt;
                                      &lt;td&gt;{{ $emp-&gt;phoneNumber }}&lt;/td&gt;
                                      &lt;td&gt;{{ $emp-&gt;address }}&lt;/td&gt;
  
  
                                      &lt;td&gt;
                                          {{--@if ( in_array($nam-&gt;isActive, array('Yes','No')) )--}}
  
                                          &lt;div class="btn-group"&gt;
                                              &lt;button type="button" class="btn btn-info"&gt;Action&lt;/button&gt;
                                              &lt;button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown"&gt;
                                                  &lt;span class="caret"&gt;&lt;/span&gt;
                                                  &lt;span class="sr-only"&gt;Toggle Dropdown&lt;/span&gt;
                                              &lt;/button&gt;
                                              &lt;ul class="dropdown-menu" role="menu"&gt;
                                                  {{--@if ($nam-&gt;isActive == 'Yes')--}}
                                                  &lt;li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $emp-&gt;employeeID }}"&gt;&lt;a href="{{ url('myform/edit/'.$emp-&gt;employeeID) }}"&gt;View/ Edit&lt;/a&gt;
                                                  &lt;/li&gt;
                                                  {{--@endif--}}
                                                  &lt;li&gt;&lt;a href="{{ url('/myform/delete/'.$emp-&gt;employeeID)}}"&gt;Delete&lt;/a&gt;&lt;/li&gt;
                                              &lt;/ul&gt;
                                          &lt;/div&gt;
                                          {{--@endif--}}
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              @endforeach
                              &lt;/tbody&gt;
                          &lt;/table&gt;
                          {{$employer-&gt;links()}}
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
      {{--&lt;a href="{{ url('user/add') }}"&gt; &lt;input type="submit" id="add" name="add" value="Edit" class="button"&gt;&lt;/a&gt;--}}
  
      &lt;/br&gt;
  
  
  
  
      &lt;h4&gt;Create a new Employee&lt;/h4&gt;
      {{--&lt;form class="templatemo-preferences-form" role="form" method="POST" action="{{ action('UserController@save') }}"&gt;--}}
      {{--&lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;--}}
  
      &lt;form role="form" method="POST" action="{{ url('myform/index') }}"&gt;
          &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;
  
  
          &lt;div class="row"&gt;
              &lt;div class="col-md-6 margin-bottom-15"&gt;
  
                  &lt;input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID"&gt;
              &lt;/div&gt;
              &lt;div class="row templatemo-form-buttons"&gt;
                  &lt;div class="submit-button"&gt;
                      &lt;button type="submit" class="btn btn-primary"&gt;New&lt;/button&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
      {{--&lt;/form&gt;--}}
  
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function() {
              $('#example').dataTable();
          } );
      &lt;/script&gt;
  
  
  @endsection
  </code></pre>
  
  <p>"edit.blade.php" is</p>
  
  <pre><code> @extends('app')
  
  @section('content')
  
      &lt;div class="templatemo-content-wrapper"&gt;
          &lt;div class="container"&gt;
              &lt;ol class="breadcrumb"&gt;
                  &lt;li&gt;&lt;a href="{{ url("/") }}"&gt;&lt;font color="green"&gt;Home&lt;/font&gt;&lt;/a&gt;&lt;/li&gt;
                  &lt;li class="active"&gt;Employee Form&lt;/li&gt;
              &lt;/ol&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col-md-8 col-md-offset-2"&gt;
                      &lt;div class="panel panel-success"&gt;
                          &lt;div class="panel-heading"&gt;Employee Form&lt;/div&gt;
                          &lt;div class="panel-body"&gt;
                              @if (count($errors) &gt; 0)
                                  &lt;div class="alert alert-danger"&gt;
                                      &lt;strong&gt;Whoops!&lt;/strong&gt; There were some problems with your input.&lt;br&gt;&lt;br&gt;
                                      &lt;ul&gt;
                                          @foreach ($errors-&gt;all() as $error)
                                              &lt;li&gt;{{ $error }}&lt;/li&gt;
                                          @endforeach
                                      &lt;/ul&gt;
                                  &lt;/div&gt;
                              @endif
  
                              &lt;form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}"&gt;
                                  &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;
  @foreach($user as $use)
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Employee ID&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="employeeID" value="{{ $use-&gt;employeeID }}" placeholder="Enter employee ID"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;E_number&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="employeeNo" value="{{ $use-&gt;employeeNo}}" placeholder="Enter employee number"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Name&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="Cname" value="{{ $use-&gt;Cname }}" placeholder="Enter Contact Name"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;DOB&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="date" class="form-control" name="dob" value="{{ $use-&gt;dob }}" placeholder="Enter date of birth"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Contact Phone&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="phoneNumber" value="{{ $use-&gt;phoneNumber }}" placeholder="Enter Mobile Number"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
  
  
                                  &lt;div class="form-group"&gt;
                                      &lt;label class="col-md-4 control-label"&gt;Address&lt;/label&gt;
                                      &lt;div class="col-md-6"&gt;
                                          &lt;input type="text" class="form-control" name="address" value="{{ $use-&gt;address }}" placeholder="Enter Address"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="form-group"&gt;
                                      &lt;div class="col-md-6 col-md-offset-4"&gt;
                                          &lt;button type="submit" class="btn btn-warning"&gt;&lt;a href="{{ url('myform/update/'.$use-&gt;employeeID) }}"&gt;
                                              Update&lt;/a&gt;
                                          &lt;/button&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  {{--&lt;a href="{{ url('myform/index') }}"&gt; &lt;input type="button" id="add" name="add" value="View data" class="button"&gt;&lt;/a&gt;--}}
  
      @endforeach
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>"myformController.php" is</p>
  
  <pre><code>  &lt;?php
  
  
  namespace App\Http\Controllers;
  
  use App\myform;
  use Mail;
  use Illuminate\Support\Facades\DB;
  use Faker\Provider\DateTime;
  use App\User;
  use App\Http\Requests\createUserRequest;
  use Illuminate\Support\Facades\Auth;
  use Illuminate\Support\Facades\Validator;
  use Illuminate\Support\Facades\Input;
  use Symfony\Component\HttpFoundation\Request;
  
  class myformController extends Controller
  {
      public $type = 'myform';
  
  
      public function getIndex()
      {
  //        $user = DB::table('user')-&gt;get();
          $employer = DB::table('employee')-&gt;simplePaginate(5);
          return view('myform.index')-&gt;with('employer',$employer);
      }
  
  
      public function formInsert()
      {
          $postform = Input::all();
          //insert data into mysql table
          $data =      array('employeeID'=&gt; $postform['employeeID'],
              'employeeNo'=&gt; $postform['employeeNo'],
              'Cname'=&gt; $postform['Cname'],
              'dob'=&gt; $postform['dob'],
              'phoneNumber'=&gt; $postform['phoneNumber'],
              'address'=&gt; $postform['address'],
  
  
          );
          //  echo print_r($data);
          $ck = 0;
          $ck = DB::table('employee')-&gt;Insert($data);
          //echo "Record Added Successfully!";
          $employer = DB::table('employee')-&gt;simplePaginate(10);
          return view('myform.index')-&gt;with('employer',$employer);
  
  
      }
  
      public function delete($id)
      {
          DB::table('employee')-&gt;where('employeeID', '=', $id)-&gt;delete();
          $employer = DB::table('employee')-&gt;simplePaginate(10);
          return view('myform.index')-&gt;with('employer', $employer);
      }
  
      public function formIDinsert()
      {
          $postform = Input::all();
          //insert data into mysql table
          $data =      array('employeeID'=&gt; $postform['employeeID'],
  
          );
          //  echo print_r($data);
          $ck = 0;
          $ck = DB::table('employee')-&gt;Insert($data);
          //echo "Record Added Successfully!";
          $employer = DB::table('employee')-&gt;simplePaginate(10);
          return view('myform.index')-&gt;with('employer',$employer);
  
  
      }
  
      public function edit($id)
      {
          try {
              //Find the user object from model if it exists
            $user=DB::table('employee')-&gt;where('employeeID', '=', $id)-&gt;get();
              //$user = User::findOrFail($id);
              //Redirect to edit user form with the user info found above.
            return view('myform.edit')-&gt;with ('user', $user);
  
              //return view('myform.edit')-&gt;with('user', myform::find($id));
          } catch (ModelNotFoundException $err) {
              //redirect to your error page
          }
      }
  
      // Update user
      public function update(Request $request, $id)
      {
          try{
              //Find the user object from model if it exists
              $user= myform::findOrFail($id);
              DB::table('employee')
                  -&gt;where('employeeID', $id)
                  -&gt;update(['employeeNo' =&gt;$request['employeeNo'],
                  'Cname'=&gt;$request['Cname'],
                  'phoneNumber'=&gt;$request['phoneNumber'],
                  'address'=&gt;$request['address']
                  ]);
              //Set user object attributes
              //the $request index should match your form field ids!!!!!
              //you can exclude any field you want.
  
  //            $user-&gt;employeeNo = $request['employeeNo'];
  //            $user-&gt;Cname = $request['Cname'];
  //            $user-&gt;phoneNumber = $request['phoneNumber'];
  //            $user-&gt;address = $request['address'];
  
              //Save/update user.
              $user-&gt;save();
              return view('myform.index')-&gt;with('user', $user);
              //redirect to somewhere
          }
          catch(ModelNotFoundException $err){
              //Show error page
          }
      }
  
  
  }
  </code></pre>
  
  <p>model "myform.php" is</p>
  
  <pre><code>   &lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class myform extends Model
  {
  
      protected $table = 'employee';
      //protected $primaryKey = 'employeeID';
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'employeeID',
          'employeeNo',
          'Cname',
          'dob',
          'phoneNumber',
          'address',
  
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  }
  </code></pre>
  
  <p>Routes.php is</p>
  
  <pre><code>    Route::any('myform', function()
  {
      return view('myform/myform');
  });
  
  Route::any('myform/myform', 'myformController@formInsert');
  Route::any('myform/index', 'myformController@getIndex');
  //to show edit form and fetch passed user id info from db
  Route::get('myform/edit/{id}', 'myformController@edit');
  
  //to get the edited info and save it to db
  Route::get('myform/update/{id}', 'UserController@update');
  Route::any('myform/index', 'myformController@formIDinsert');
  Route::any('myform/delete/{id}', 'myformController@delete');
  </code></pre>