Laravel – inserting foreign keys causes Integrity constraint violation

I have two related models Domainand Server. I’m trying to insert data to my tables using a form. here is my store function :

public function store(Request $request, Domain $domain, Server $server)


    return redirect()->route('domains.index');

the table servers has a FK domain_id that points to the PK
Once I submit my form i get the error :

SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘domain_id’ cannot be null (SQL: insert into servers (domain_id, updated_at, created_at) values (, 2015-11-25 10:55:45, 2015-11-25 10:55:45))

It seems like fk is not correctly linked to the pk..but I don’t know how to solve this. thanks 🙂

Notes :

1 – My 2 related models:

Class Server extends Eloquent {
    public function domain(){
    return $this->belongsTo('Domain');
// $fillable and stuff..

Class Domain extends Eloquent {
    public function servers(){
    return $this->hasMany('Server');

2 – My tables are related:
Schema::table('servers', function($table){


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Have you tried?

  $new_domain = $domain->create($request->all());


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x