@hiiro check topic_id is in the list of fillable fields
And I think it should be
'topic_id' => $topic->id
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
when user want to reply on the topic, this appear:
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'topic_id' cannot be null (SQL: insert into `replies` (`topic_id`, `content`, `user_id`, `updated_at`, `created_at`) values (?, <div>test</div>, 1, 2022-02-08 04:15:38, 2022-02-08 04:15:38))
here is my code :
public function store(CreateReplyRequest $request, Topic $topic)
{
$user = auth()->user();
$user->replies()->create([
'topic_id' => $topic->topic_id,
'content' => $request->content
]);
return redirect()->back();
}
public function replies()
{
return $this->hasMany(Reply::class);
}
<div class="card my-5">
<div class="card-header">
Reply
</div>
<div class="card-body">
@auth
<form action="{{route('replies.store',$topic->slug)}}" method="POST">
@csrf
<input type="hidden" name="content" id="content">
<trix-editor input="content"></trix-editor>
<button type="submit" class="btn btn-sm my-2 btn-success">
Add Reply
</button>
</form>
@else
<a href="{{route('login')}}" class="btn btn-info">Sign in to add a reply</a>
@endauth
</div>
</div>
@Hiiro I would recommend that you read up on best practice for naming columns in the database.. The primary key should just be id not topicID
Anyways. This would then be the correct way of getting the key
$user->replies()->create([
'topic_id' => $topic->topicID, //fixed
'content' => $request->content
]);
Please or to participate in this conversation.