I'm still new to laravel (5.5) and I'm having a problem when I update user records, the error is:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'department' cannot be null (SQL: update
users
setpassword
= $2y$10$j5wfEZ3N7SYDzWA10hbAguY9o6jY.xk1vfUEw0mBmc0351ZHaYDiy,department
= ,contactno
= 12345,updated_at
= 2018-09-11 00:52:20,isAdmin
= whereid
= 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');
}
updateuserblade.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>
UpdateDataRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateDataRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; }
/**
* 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
allow null in your database, any field that you can with no value – Beginner$dept->iso_code
– Beginnerdepartement
is it int or string ? – Beginner