I have a basic bit of code that lets you pick from a drop-down which then gets used as a foreign key on the model. I've got a test which should pass, but I keep getting an error :
1) CourseTest::test_can_edit_a_valid_course
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add
or update a child row: a foreign key constraint fails (`homestead`.`courses`, CONSTRAINT
`courses_discipline_id_foreign` FOREIGN KEY (`discipline_id`) REFERENCES `disciplines` (`id`))
(SQL: insert into `courses` (`code`, `title`, `discipline_id`, `updated_at`, `created_at`)
values (TEST3985, quos sit ullam, {"code":"TEST_odit","title":"et nesciunt voluptas","updated_at":"2016-06-01
12:41:55","created_at":"2016-06-01 12:41:55","id":22}, 2016-06-01 12:41:55, 2016-06-01 12:41:55))
It looks to me like it's setting the "discipline_id" to be an actual copy of the "discipline" model entry rather than just it's 'id'. The code works fine in a browser, but fails like that with phpunit. The controller action is a simple :
public function update(Request $request, $id)
{
$course = Course::findOrFail($id);
$course->fill($request->all())->save();
return redirect()->route('course.show', $id);
}
There's no relations set on the models yet. The html for the select to pick the discipline_id is just :
<div class="form-group">
<select class="form-control" name="discipline_id">
@foreach ($disciplines as $discipline)
<option value="{{ $discipline->id }}" @if ($course->discipline_id == $discipline->id) selected @endif>{{ $discipline->title }}</option>
@endforeach
</select>
</div>
Anyone else hit this? It's a bit puzzling...
Edit: this is a L5.2 app.