I am getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '800' for key 'PRIMARY' (SQL: insert into
field_goal_distances(athlete_id,distance,game_id,id) values (12344, 900, 39, 800), (12344, 900, 39, 900))
I understand that it's trying to write 800 or 900 into the id column and that's why I'm getting the error. What I don't understand is where I've told it to do that.
I have 2 tables: field_goals & field_goal_distances A user can enter they kicked 2 field goals. Then I show 2 text inputs so they can enter the distance(s).
Everything is working great until I try to attach the distance values.
Controller
// distances is an array [0]=>45, [1]=>33 etc.
if ($request->distances) {
$game = FieldGoal::find($field_goals->id);
foreach ($request->distances as $distance) {
$game->distances()->attach($request->distances, [
'athlete_id' => $request->athlete_id,
'distance' => $distance,
]);
}
}
FieldGoal.php
public function distances()
{
return $this->belongsToMany('App\FieldGoalDistance', 'field_goal_distances', 'game_id', 'id');
}
FieldGoalDistance.php
public function field_goal()
{
return $this->belongsToMany('App\FieldGoal');
}
I don't understand why it wants to write to the ID column since it's an auto-incrementing column:
Migration
$table->bigIncrements('id');
$table->bigInteger('athlete_id')->unsigned();
$table->bigInteger('game_id')->unsigned();
$table->smallInteger('distance');
$table->timestamps();
$table->foreign('athlete_id')->references('id')->on('athletes')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('game_id')->references('id')->on('field_goals')->onDelete('cascade')->onUpdate('cascade');
Thank you for any suggestions!
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kI7J7F
via IFTTT
Aucun commentaire:
Enregistrer un commentaire