-1

I'am trying to insert a date to a table but I got this message:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('stagesite'.'internships', CONSTRAINT 'internships_contact_id_foreign' FOREIGN KEY

This is my code:

public function store( Request $request) 
{ 
    $input = $request->all(); 
    $input['status_id'] = 1; 
    internship::create($input); 
    dd(' success '); 
} 

And that's my form:

<div class="form-group{{ $errors->has('contact') ? 'has-error' : '' }}">
    {{ form :: select('course_id',$courses , null , ['id' => 'course_id', 'class' =>'form-control'] ) }} 
</div> 
<div class="form-group{{ $errors->has('contact') ? 'has-error' : '' }}"> 
    {{ form :: hidden('contact_id',$contacts , null , ['id' => 'contact_id', 'class' =>'form-control'] ) }} 
</div>
Stacked
  • 6,892
  • 7
  • 57
  • 73
mohamed gaber
  • 72
  • 1
  • 2
  • 11
  • public function store( Request $request) { $input = $request->all(); $input['status_id'] = 1; internship::create($input); dd(' success '); } – mohamed gaber Jun 16 '16 at 10:05
  • and that is my form
    {{ form :: select('course_id',$courses , null , ['id' => 'course_id', 'class' =>'form-control'] ) }}
    {{ form :: hidden('contact_id',$contacts , null , ['id' => 'contact_id', 'class' =>'form-control'] ) }}
    – mohamed gaber Jun 16 '16 at 10:05
  • 1
    Edit your question and update your code there. – Arulkumar Jun 16 '16 at 10:11
  • 1
    Duplicate: check this [SO post](http://stackoverflow.com/questions/17648179/sqlstate23000-integrity-constraint-violation-1452-cannot-add-or-update-a-chi) – Stacked Jun 16 '16 at 11:28
  • First make sure that you have added the 'contact_id' into $fillable on `internship` model. Then check the value of `$contacts` variable on your form – Ravisha Hesh Jun 16 '16 at 14:52

1 Answers1

0

First of all See your error before asking help.

You have set foreign key relationship in Database. And When you are trying to insert data, Relation is not match with foreign key and you are getting this error.

You must take care of foreign key relation and must insert same data as in relation with other table.

You can get more idea about relationship with laravel by this.

you can also get more idea how to insert data through Laravel controller by this.

Ketav
  • 760
  • 1
  • 8
  • 27