在 laravel 中更新时,我的选择表单返回 null
Posted
技术标签:
【中文标题】在 laravel 中更新时,我的选择表单返回 null【英文标题】:My select form returns null when updating in laravel 【发布时间】:2019-02-15 10:53:15 【问题描述】:我还是 laravel (5.5) 的新手,更新用户记录时遇到问题,错误是:
sqlstate [23000]:完整性约束违规:1048列'部门'不能为null(SQL:UPDATE
password
password
= $ 2Y $ 10 $ J5WFEZ3NYDZWA10HBAGUY9O6JY.XK1VFUEW0MBMC0351ZHYDIY,department
= 12345 ,updated_at
= 2018-09-11 00:52:20,isAdmin
= 其中id
= 1)
recordscontroller.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');
更新userblade.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>
更新数据请求.php
<?php
命名空间 App\Http\Requests;
使用 Illuminate\Foundation\Http\FormRequest;
类 UpdateDataRequest 扩展 FormRequest /** * 确定用户是否有权提出此请求。 * * @return 布尔值 */ 公共函数授权() 返回真;
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
return [
//
'wholename' => 'required|string|max:255',
'department' => 'required|string|max:255',
'name' => 'required|string|max:255',
'password' => 'required|string|min:6|confirmed',
];
【问题讨论】:
使您的列department
允许在您的数据库中为空,任何您可以无值的字段
但它不应该为空。部门列从 w/c 的选择中获取它的数据由另一个表填充。尽管它在更新期间显示了一个选定的选项,但它返回为 null
如果不为空,请尝试检查每个选项的值$dept->iso_code
它不是空的。猜我的问题是控制器。但我还是想不通。
你的专栏departement
的结构是int还是string?
【参考方案1】:
在public function update
方法中尝试dd($request);
。
【讨论】:
$request->get('department') 的值来自 updateuser 刀片中的选择表单。顺便问一下 dd($request) 有什么用? 你能检查 $request->get('department') 是否有值?对不起我的解释:)。 dd(value) 用于调试value
它就像var_dump($variable); exit();
但更漂亮的风格
+request: ParameterBag #236 ▼ #parameters: array:9 [▼ "_token" => "FA0z7Z5dXmEVNwhwURSN0dmgAwPjc5zKuj9iWOFz" "_method" => "PATCH" "wholename" => "OJT" "contact " => "12345" "部门" => "ICT" "姓名" => "admin1" "password" => "admin2" "password_confirmation" => "admin2" "userlvl" => "1" ] 跨度>
它有,它尝试再次更新它,但我在数据库中的部门列返回“1”而不是选择的值,更新后的值变成了“1”
它不是空的。猜我的问题是控制器。但我还是想不通。【参考方案2】:
在控制器上:
$Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;
$Record->department = $request->get('bacs');
冗余....... 改成这样:
$Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;
看在上帝的份上。很抱歉给您带来麻烦。
【讨论】:
以上是关于在 laravel 中更新时,我的选择表单返回 null的主要内容,如果未能解决你的问题,请参考以下文章
将表单选择的数据传递给 laravel7 中的控制器,返回 null