我还是laravel(5.5)的新手,更新用户记录时遇到问题,错误是:
SQLSTATE [23000]:违反完整性约束:1048列“部门”不能为空(SQL:更新
users
设置password
= $ 2y $ 10 $ j5wfEZ3N7SYDzWA10hbAguY9o6jY.xk1vfUEw0mBmc0351ZHaYyy =,{{1 ,department
= 12345,contactno
= 2018-09-11 00:52:20,updated_at
=其中isAdmin
= 1)
recordscontroller.php
id
updateuserblade.php
public function edit($id)
{
//
$edit_form = User::Join('office', 'users.department', '=', 'office.iso_code')->find($id);
$records = User::all();
$dept = Office::all();
$result = DB::table('users')
->where('isBACSec','=', '1')
->get();
return View('updateuser')->with('edit_form',$edit_form)->with('records',$records)->with('dept',$dept)->with('result',$result);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateDataRequest $request,$id)
{
//
$Record=User::find($id);
$Record->wholename = $request->get('wholename');
$Record->name = $request->get('name');
$Record->contactno = $request->get('contact');
$Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;
$Record->department = $request->get('bacs');
$Record->isAdmin = $request->get('usrlvl');
$Record->save();
return redirect()->back();
$request->session()->flash('flash_message','Record updated successfully');
}
UpdateDataRequest.php
<div class="container">
<div class="row">
<!-- registration form -->
<div class="col-xs-5 col-md-5">
<div class="panel panel-default">
<div class="panel-heading">Update Record</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ url('/edited_data',$edit_form->id) }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group{{ $errors->has('wholename') ? ' has-error' : '' }}">
<label for="wholename" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="wholename" type="text" class="form-control" name="wholename" value="{{ $edit_form->wholename }}" required autofocus>
@if ($errors->has('wholename'))
<span class="help-block">
<strong>{{ $errors->first('wholename') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('contact') ? ' has-error' : '' }}">
<label for="contact" class="col-md-4 control-label">Contact</label>
<div class="col-md-6">
<input id="contact" type="text" class="form-control" name="contact" value="{{ $edit_form->contactno }}" required autofocus>
@if ($errors->has('contact'))
<span class="help-block">
<strong>{{ $errors->first('contact') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('department') ? ' has-error' : '' }}">
<label for="department" class="col-md-4 control-label" >Department</label>
<div class="col-md-6">
<select id="department" type="text" class="form-control" name="department" onchange="showDiv(this)" required autofocus>
@foreach ($dept as $key=>$dept)
<option value="{{$dept->iso_code}}" @if($edit_form->department == $dept->iso_code) selected @endif>{{$dept->office_name}}</option>
@endforeach
</select>
@if ($errors->has('department'))
<span class="help-block">
<strong>{{ $errors->first('department') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ $edit_form->name }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group{{ $errors->has('userlvl') ? ' has-error' : '' }}">
<label for="userlvl" class="col-md-4 control-label" >User Level</label>
<div class="col-md-6">
<select id="userlvl" type="text" class="form-control" name="userlvl" autofocus>
<option value="" >User</option>
<option id="admin" style="display:none;" value = "1" @if($edit_form->isAdmin === 1)selected @endif>Admin</option>
<script type="text/javascript">
function showDiv(elem){
if(elem.value == "ICT"){
document.getElementById('admin').style.display = 'block';
}else{
document.getElementById('admin').style.display = 'none';
}
}
</script>
</select>
@if ($errors->has('userlvl'))
<span class="help-block">
<strong>{{ $errors->first('userlvl') }}</strong>
</span>
@endif
</div>
</div>
@if ($edit_form->isBACSec == 1)
<div class="form-group">
<div class="checkbox col-md-8">
<label><input name="bacs" id="bacs" type="checkbox" value="1" onchange="alert('This user is no longer the BAC Secretariat')" checked>Is BAC Secretariat?</label>
</div>
</div>
@elseif($result->isEmpty())
<div class="form-group">
<div class="checkbox col-md-8">
<label><input name="bacs" id="bacs" type="checkbox" value="1">Assign as BAC Secretariat</label>
</div>
</div>
@endif
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Update
</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- update edit edelete-->
@include('usertable')
</div>
命名空间App \ Http \ Requests;
使用Illuminate \ Foundation \ Http \ FormRequest;
类UpdateDataRequest扩展了FormRequest { / ** *确定用户是否有权发出此请求。 * * @返回布尔 * / 公共功能authorize() { 返回true; }
<?php
}
答案 0 :(得分:0)
$request->get('department')
的值是什么?
在dd($request);
方法内尝试public function update
。
您可以在UpdateDataRequest
内共享代码吗?
答案 1 :(得分:0)
在控制器上:
df = df.sort_values(by = ‘Profitability’, ascending = False)
df.head(10)
冗余..... 改为:
$Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;
$Record->department = $request->get('bacs');
看在上帝的份上。对不起,麻烦。