更新记录在桌子上

时间:2018-06-13 10:03:46

标签: php laravel

我有这些表格,我想用其他数据更新表格......但即使我选择另一列,它也只更新1条记录。请帮助谢谢。

// The call to the modal

<a href="#custom-modal" class="btn btn-primary btn-md waves-effect waves-light"
                               data-animation="fadein" data-id="{{$value->id}}" data-plugin="custommodal" data-overlaySpeed="200" data-overlayColor="#36404a">
 <i class="glyphicon glyphicon-refresh "></i> Update 
</a>

// method on modal

<form  role="form" data-parsley-validate novalidate method="POST" action="{{url('hr/updateRecord/'. $value->id)}}">

// My controller function 
 public function updateStaff(Request $request)
    {
        $staff = User::find($request->id);
        $staff->salary = $request->salary;
        $staff->phone = $request->phone;
        $staff->age = $request->age;
        $staff->startDate = $request->date;
        $staff->office = $request->office;
        $staff->save();

Session::flash('message','Successfully Updated '.$request->firstname.'s record');
       return redirect('hr/staffdetails');

    }

// route
Route::post('/updateRecord/{id}','HrController@updateStaff');

if i try updating record of rope for instance... it's nana's data that would be updated and it's the only record that is updated no matter the name i select

//表格的刀片

<div class="bottom-table">
                <div class="table table-responsive">
                <table class="table table-striped table-bordered" id="table">
                        <thead>
                            <tr>
                              <th>First Name</th>
                              <th>Last Name</th>
                              <th>Position</th>
                              {{--  <th>Department</th>  --}}
                              <th>Phone number</th>
                              <th>Office</th>
                                <th>Age</th>
                              <th>Start Date</th>
                                <th>Salary</th>
                                <th><center>Action</center></th>
                            </tr>
                        </thead>
                        {{ csrf_field() }}
                        <tbody>
                        @foreach($staff as $value)

                            <tr>
                              <td>{{$value->firstname}}</td>
                              <td>{{$value->lastname}}</td>
                              <td>{{$value->position}}</td>
                              {{--  <td>{{$value->department_id}}</td>  --}}
                              <td>{{$value->phone}}</td>
                              <td>{{$value->office}}</td>         
                              <td>{{$value->age}}</td>
                              <td>{{$value->startDate}}</td>
                               <td>N{{$value->salary}}</td>
                               <td><center>
                            <a href="#custom-modal" class="btn btn-primary btn-md waves-effect waves-light"
                               data-animation="fadein" data-id="{{$value->id}}" data-plugin="custommodal" data-overlaySpeed="200" data-overlayColor="#36404a">
                                <i class="glyphicon glyphicon-refresh "></i> Update 
                            </a>
                            <a href="attendance.html" class="show-modal btn btn-info btn-sm" data-id="{{$value->id}}">
                                <i class="glyphicon glyphicon-time"></i> Attendance
                            </a>
                            </center>
                        </td>  
                            </tr>

                        @endforeach

                        </tbody>
                    </table>

//用于更新的模式

<div id="custom-modal" class="modal-demo">

       <button type="button" class="close" onclick="Custombox.close();">
                        <span>&times;</span><span class="sr-only">Close</span>

                    </button>

                    <h4 class="custom-modal-title">Update staff report</h4>

                    <div class="custom-modal-text text-left">



<form  role="form" data-parsley-validate novalidate method="POST" action="{{url('hr/updateRecord/'.$value->id)}}">
                 <input type="hidden" name="_token" value="{{csrf_token()}}"/>

                            <div class="form-group">
                                <label for="name">Age</label>
                                <input type="number" class="form-control" id="name" name="age" required parsley-trigger="change">
                            </div>

                            <div class="form-group">
                                <label for="name">Phone number </label>
                                <input type="number" class="form-control" id="phone" name="phone" required parsley-trigger="change">
                            </div>

                            <div class="form-group">
                                <label for="name">Office</label>
                                <input type="text" class="form-control" id="office" name="office"  required parsley-trigger="change">
                            </div>

                            <div class="form-group">
                                <label for="name">Salary </label>
                                <input type="number" class="form-control" id="salary" name="salary" placeholder="" required parsley-trigger="change">
                            </div>

                            <div class="form-group">
                                <label for="assign">Start date</label>
                                <input type="text" class="form-control" name="date" id="datepicker" placeholder="format yyyy-mm-dd" required parsley-trigger="change">
                            </div>

                            <button type="submit" class="btn btn-success waves-effect waves-light">Save</button>

                            <button type="button" class="btn btn-danger waves-effect waves-light m-l-5">Cancel</button>

                        </form>
                    </div>

1 个答案:

答案 0 :(得分:2)

如果您想进行批量更新

$data = [
    'salary' =>  $request->salary,
    'phone' => $request->phone,
    'age' => $request->age,
    'startDate' => $request->date,
    'office' => $request->office,
];

// or $data = $request->all();
User::where('your-condition')->update($data); // it returns updated record counts

同时检查用户模型:

protected $fillable = ['your columns'];

文档https://laravel.com/docs/5.5/eloquent#updates