gpt4 book ai didi

php - 在 laravel 中更新时我的选择表单返回 null

转载 作者:行者123 更新时间:2023-11-29 17:00:52 25 4
gpt4 key购买 nike

我还是 laravel (5.5) 的新手,在更新用户记录时遇到问题,错误是:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'department' cannot be null (SQL: update users set password = $2y$10$j5wfEZ3N7SYDzWA10hbAguY9o6jY.xk1vfUEw0mBmc0351ZHaYDiy, department = , contactno = 12345, updated_at = 2018-09-11 00:52:20, isAdmin = where id = 1)

记录 Controller .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{ /** * 确定用户是否有权提出此请求。 * * @返回 bool 值 */ 公共(public)函数授权() { 返回真; }

/**
* 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',
];
}

}

最佳答案

public function update方法中尝试dd($request);

关于php - 在 laravel 中更新时我的选择表单返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52267370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com