mardi 27 juin 2017

Laravel eloquent Cannot add or update a child row: a foreign key constraint fails

Hi so here are my relation:

Location:

public function _geoloc()
{
    return $this->belongsTo('App\GeoLoc',  'id');
}

Geoloc:

public function locations()
{
    return $this->hasOne('App\Location', 'geoloc_id');
}

Controller:

    $geoloc = new Geoloc;
    $geoloc->lat = $request->input('lat');
    $geoloc->lng = $request->input('lng');
    $geoloc->save();
    $geoloc_id = $geoloc->id;

    $location = new Location;
    $location->business_id = $business_id;
    $location->latitude = $request->input('lat');
    $location->longitude = $request->input('lng');
    $location->slug = $request->input('name');
    $location->geoloc_id = $geoloc_id;
    $location->save();

Full error message:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (liveoldham.locations, CONSTRAINT locations_geoloc_id_foreign FOREIGN KEY (geoloc_id) REFERENCES geoloc (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into locations (business_id, latitude, longitude, slug, geoloc_id, updated_at, created_at) values (1, 53.4867546, -2.054066599999942, Live & Now, 94, 2017-06-27 15:22:51, 2017-06-27 15:22:51))

All the values are correct, row is inserted into geoloc but it doesn't get inserted to locations and this is the error I am gettiing, how can that be fixed?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire