Laravel: Integrity constraint violation: 1452 Cannot add oder update a child row: a foreign key constraint fails

ich habe diesen Fehler auf meinem Laravel Anwendung:

Kann nicht hinzufügen oder aktualisieren ein child row: a foreign key constraint fails

Hier meine 2 Tabellen

Benutzer

Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username');
        $table->string('password');
        $table->string('firstname');
        $table->string('lastname');
        $table->string('middlename');
        $table->string('address');
        $table->string('birthday');
        $table->string('contact');
        $table->string('email');
        $table->enum('isAdmin', array(0,1))->default(0);
        $table->enum('isTeacher', array(0,1))->default(0);
        $table->timestamps();

    });

Teilnahme

Schema::create('attendance', function(Blueprint $table)
        {
        $table->increments('id');
        $table->string('status');
        $table->string('comment');
        $table->integer('student_id')->unsigned();
        $table->foreign('student_id')->references('id')->on('users');
        $table->string('student_firstname');
        $table->string('student_lastname');
        $table->integer('teacher_id')->unsigned();
        $table->foreign('teacher_id')->references('id')->on('users');
        $table->string('teacher_firstname');
        $table->string('teacher_lastname');
        $table->timestamps();

Hier meine Modelle

Benutzer

class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');


public function attendance()
{

        return $this->belongsTo('Attendance');


}

Teilnahme

class Attendance extends Eloquent
{






    public function users()
    {

        return $this->hasMany('User');

    }


}

**Und schließlich, hier ist mein controller **

    public function postCreateAttendance()
{
    $validate = Validator::make(Input::all(), array(
        'status' => 'required'
    ));


    if ($validate->fails())
    {
        return Redirect::route('viewStudent')->withErrors($validate)->withInput();
    }
    else
    {

        //$student  = User::whereRaw('isTeacher = "0" and isAdmin = "0"')->get();

        //$teacher  = User::whereRaw('isTeacher = "1" and isAdmin = "0"')->get();

        $student = new User();
        $teacher = new User();
        $attendance = new Attendance();
        $student->save();
       $teacher->save();
       $attendance->save();



        $student->student_id = Input::get('3');
        $attendance->status = Input::get('status');
        $attendance->comment = Input::get('comment');
        $attendance->student_firstname = Input::get('student_first');
        $attendance->student_lastname = Input::get('student_last');
        $attendance->teacher_firstname = Input::get('teacher_first');
        $attendance->teacher_lastname = Input::get('teacher_last');

Ich habe versucht dies auch so gut und Ihr gibt mir den gleichen Fehler.

public function sampleAttendance()

{

    User::find(1)->attendance()->insert(array(
        'status' => 'present',
        'comment' => 'none',
        'student_id' => '1',
        'student_firstname' => 'student_first',
        'student_lastname' => 'student_last',
        'teacher_firstname' => 'teacher_first',
        'teacher_lastname' => 'teacher_last'


        ));

Bitte helfen? 🙁

InformationsquelleAutor Virgil Cruz | 2015-03-09
Schreibe einen Kommentar