Make sure in model to tell it the primary key if not id. Like
protected $primaryKey = 'some_id';
// or
protected $primaryKey = 'whateverid';
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am getting this error
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`multiauth_5.2`.`students`, CONSTRAINT `students_dept_id_foreign` FOREIGN KEY (`dept_id`) REFERENCES `departments` (`dept_id`) ON DELETE CASCADE) (SQL: insert into `students` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Muhammad Arslan, m.arslan2037@gmail.com, 123456, 2016-07-14 13:18:08, 2016-07-14 13:18:08))
here is route
Route::get('admin/addStudents', 'AdminController@student');
Route::post('admin/addStudents', 'AdminController@addStudent');
here is controller method for this
public function addStudent(Request $request)
{
$this->validate($request, [
'roll_no' => 'required|min:1',
'name' => 'required|min:2',
'father_name' => 'required|min:3',
'address' => 'required|min:3',
'cnic' => 'required|min:13',
'phone_no' => 'required|min:11',
'father_phone_no' => 'required|min:11',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
'dept_id' => 'required|min:1',
]);
Student::create($request->except('_token'));
return redirect('admin/showStudents');
}
model look like this
protected $fillable = [
'roll_no',
'name',
'father_name',
'address',
'cnic',
'phone_no',
'father_phone_no',
'email',
'password',
'dept_id',
];
you can see in error there is no value for dept_id but in the form i enter this value you can check it in form
<div class="form-group">
{!! Form::label('roll_no','Roll No:') !!}
{!! Form::text('roll_no', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('name','Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('father_name','Father Name:') !!}
{!! Form::text('father_name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('address','Address:') !!}
{!! Form::text('address', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('cnic','CNIC:') !!}
{!! Form::text('cnic', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone_no','Phone No:') !!}
{!! Form::text('phone_no', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('father_phone_no','Father\'s Phone No:') !!}
{!! Form::text('father_phone_no', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email','Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
<label for="password">Password</label>
<div>
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<label for="password_confirmation">Confirm Password:</label>
<div>
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>
<div class="form-group">
{!! Form::label('dept_id','Department ID:') !!}
{!! Form::text('dept_id', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-conrol']) !!}
</div>
In your controller
$departments = Departments::orderBy('name','ASC')->get();
then pass $departments to your view.
Then on your form, instead of the dept_id text field;
<select id="dept_id" name="dept_id">
<option value="" >Choose Department</option>
@foreach($departments as $department)
<option value='{{ $department->id }}'>{{ $department->name }}</option>
@endforeach
</select>
Please or to participate in this conversation.