0

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 set password = $2y$10$j5wfEZ3N7SYDzWA10hbAguY9o6jY.xk1vfUEw0mBmc0351ZHaYDiy, department = , contactno = 12345, updated_at = 2018-09-11 00:52:20, isAdmin = where 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');
}

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

}

16
  • make your column department allow null in your database, any field that you can with no value Commented Sep 11, 2018 at 1:54
  • but it shouldn't be null. the department column was getting it's data from the select from w/c is populated by another table. although it shows a selected option during updates it returns as null Commented Sep 11, 2018 at 2:01
  • try checking the value of each option if is not null $dept->iso_code Commented Sep 11, 2018 at 2:07
  • it isn't null though. guess my problem is with the controller. but I still can't figure it out. Commented Sep 11, 2018 at 2:41
  • whats the structure of your column departement is it int or string ? Commented Sep 11, 2018 at 2:49

2 Answers 2

0

try dd($request); inside public function update method.

Sign up to request clarification or add additional context in comments.

7 Comments

the value of the $request->get('department') comes from the select form in updateuser blade. what is the use of dd($request) by the way?
can you check is there a value for $request->get('department') ? sorry for my explanation :). dd(value) is for debug the value it's like var_dump($variable); exit(); but more nicest style
+request: ParameterBag {#236 ▼ #parameters: array:9 [▼ "_token" => "FA0z7Z5dXmEVNwhwURSN0dmgAwPjc5zKuj9iWOFz" "_method" => "PATCH" "wholename" => "OJT" "contact" => "12345" "department" => "ICT" "name" => "admin1" "password" => "admin2" "password_confirmation" => "admin2" "userlvl" => "1" ] }
it has, it tried updating it again but my department column in the database returns "1" instead of the selected value the updated value became "1"
it isn't null though. guess my problem is with the controller. but I still can't figure it out.
|
0

on the controller:

$Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;
$Record->department = $request->get('bacs');

Redundancy........ changed it to this:

   $Record->department = $request->get('department');
$Record->password =bcrypt($request->get('password')) ;

For goodness sake. Sorry for the troubles.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.