Smsma
1 year ago
484
9
Laravel

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

Posted 1 year ago by Smsma

"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into births (title, date, description, doctor_id, user_id, updated_at, created_at) error

birthController

public function store(Request $request) { $this->validate(request(), [ 'title' =>'required', 'date' =>'required', 'doctor_id' =>'required', 'name' =>'required', 'date' =>'required', 'password' =>'required|min:8', 'email' =>'required|email|unique:users',

     ]);
    $user = new User();
    $user->name = request('name');
    $user->email = request('email'); 
    $user->password = request('password');
    $user->save(); 

    $birth = new Birth();
    $birth->title = request('title');
    $birth->date = request('date');
    $birth->description = request('description');  
    $birth->doctor_id = request('doctor_id');
    $birth->user_id = request('user_id');
    $birth->save();
    $success = 'the birth has been created successfully';
    return redirect('/births')->with('success', $success);    
    
}

create view :

{!! Form::open(['method' => 'POST', 'files' => true,'route' => ['births.store'],'style'=>'display:inline']) !!}

                {!! Form::label('name', 'name') !!}
                {!! Form::text('name', ' ' , ['class' => 'form-control'] )!!}
                </br>
                {!! Form::label('name', 'email') !!}
                {!! Form::email('email', ' ' , ['class' => 'form-control'] )!!}
                </br>
                {!! Form::label('name', 'password') !!}
                {!! Form::password('password', ['class' => 'form-control']) !!}
                </br>
                {!! Form::label('name', 'Title') !!}
                {!! Form::text('title', ' ' , ['class' => 'form-control'] )!!}
                </br>
                {!! Form::label('name', 'Date') !!}
                {!! Form::date('date', \Carbon\Carbon::now() , ['class' => 'form-control'])!!}
                </br>
                {!! Form::label('name', 'Description') !!}
                {!! Form::text('description', ' ' , ['class' => 'form-control'] )!!}
                </br>
                {!! Form::label('name', ' Doctor Name') !!}
                {!! Form::select('doctor_id', $doctors , null, 
                ['class' => 'form-control chosen-type']) !!}
                </br>                     
                {!! Form::reset('reset', array('class'=>'btn btn-primary')) !!}
                {!! Form::submit('Add', array('class'=>'btn btn-primary')) !!}
            {!! Form::close() !!}

Please sign in or create an account to participate in this conversation.