Today we’ll store the structured fields extracted by GPT and display them in a readable format. We’ll also prepare the UI to allow admins to review and correct the AI output before finalizing.
🧩 Step 1: Add a JSON Column to Store Fields
Update your documents table:
php artisan make:migration add_extracted_fields_to_documents_table
In the migration:
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->json('extracted_fields')->nullable();
});
}
Then:
php artisan migrate
💾 Step 2: Save GPT Fields to This Column
Update your controller:
Document::create([
'title' => $request->title,
'type' => $request->type,
'filename' => $filename,
'user_id' => auth()->id(),
'extracted_text' => $text,
'extracted_fields' => $fields, // 👈 added
]);
📄 Step 3: Show Fields in the View
In resources/views/documents/show.blade.php
:
<h2>Extracted Fields</h2>
@if($document->extracted_fields)
<ul>
@foreach(json_decode($document->extracted_fields, true) as $key => $value)
<li><strong>{{ ucfirst(str_replace('_', ' ', $key)) }}:</strong> {{ $value }}</li>
@endforeach
</ul>
@else
<p>No extracted fields yet.</p>
@endif
✏️ Step 4: Add Editable Fields (Optional)
To make the fields editable, turn each into a form input:
<form method="POST" action="{{ route('documents.updateFields', $document->id) }}">
@csrf
@method('PUT')
@foreach(json_decode($document->extracted_fields, true) as $key => $value)
<label>{{ ucfirst(str_replace('_', ' ', $key)) }}</label>
<input type="text" name="fields[{{ $key }}]" value="{{ $value }}">
@endforeach
<button type="submit">Save Changes</button>
</form>
Then define a route and controller method:
Route::put('/documents/{document}/fields', [DocumentController::class, 'updateFields'])->name('documents.updateFields');
In controller:
public function updateFields(Request $request, Document $document)
{
$document->update([
'extracted_fields' => $request->input('fields'),
]);
return back()->with('success', 'Fields updated.');
}
✅ Summary
✅ Today you:
- Saved GPT-extracted fields to the DB
- Displayed them nicely in the UI
- Made them editable for admin review
✅ Up next (Day 5): We’ll build a validation checklist to flag missing fields, invalid data, and risky clauses using GPT prompts.