Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rfmapp's avatar
Level 1

Unable to get database column after sucessfull insert

I have a piece of code that handles PDF report generation. Once I have all my data ready, I make a database INSERT operation and then I pass the newly created record id to my function:

$report = PdfReport::create(
	[
		'pdf_uuid' => Str::uuid(),
		'status' => PdfReport::PENDING,
		'fromDate' => date('Y-m-d', strtotime($request->fromDate)),
		'toDate' => date('Y-m-d', strtotime($request->toDate)),
		'user_id' => $request->user()->id,
		'type' => $pdftype,
		'content' => json_encode($content),
	]
);

PdfReportJob::dispatch($report->id);

If I debug $report->content I get what is expected:

{"fromDate":"05\/01\/2024","toDate":"05\/31\/2024","reportData":{... hidden on purpose

The thing is, when I try to access that record inside my Job class, I don't see the content:

$item = PdfReport::where('id', $this->reportId)->get()->first();
Log::info(json_encode($item));
{"id":55,"pdf_uuid":"0c015eba-5beb-4ae4-bee3-f419907ce49f","status":0,"type":2,"fromDate":"2024-05-01","toDate":"2024-05-31","created_at":"2024-06-14T14:20:16.000000Z"}

Any ideas what I'm doing wrong?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It looks like the issue might be related to the way you're retrieving the record from the database. Specifically, it seems that the content column is not being retrieved. This could be due to a few reasons, such as the column not being selected or an issue with the model's $fillable or $hidden properties.

Here are a few steps to troubleshoot and resolve the issue:

  1. Check Model's $fillable Property: Ensure that the content column is included in the $fillable property of your PdfReport model.

    class PdfReport extends Model
    {
        protected $fillable = [
            'pdf_uuid',
            'status',
            'fromDate',
            'toDate',
            'user_id',
            'type',
            'content',
        ];
    }
    
  2. Check Model's $hidden Property: Ensure that the content column is not included in the $hidden property of your PdfReport model.

    class PdfReport extends Model
    {
        protected $hidden = [
            // 'content', // Make sure this line is not hiding the content column
        ];
    }
    
  3. Retrieve the Record Properly: When retrieving the record, use the first() method directly instead of chaining get() and first(). This ensures you get a single instance of the model.

    $item = PdfReport::where('id', $this->reportId)->first();
    Log::info(json_encode($item));
    
  4. Check Database Schema: Ensure that the content column exists in your database schema and that it is being populated correctly during the insert operation.

  5. Debugging: Add some debugging statements to ensure that the content column is being set correctly during the insert operation.

Here is the revised code with the above considerations:

$report = PdfReport::create(
    [
        'pdf_uuid' => Str::uuid(),
        'status' => PdfReport::PENDING,
        'fromDate' => date('Y-m-d', strtotime($request->fromDate)),
        'toDate' => date('Y-m-d', strtotime($request->toDate)),
        'user_id' => $request->user()->id,
        'type' => $pdftype,
        'content' => json_encode($content),
    ]
);

PdfReportJob::dispatch($report->id);

And in your Job class:

$item = PdfReport::where('id', $this->reportId)->first();
Log::info(json_encode($item));

By following these steps, you should be able to retrieve the content column correctly. If the issue persists, double-check your database schema and ensure that the content column is being populated as expected.

1 like

Please or to participate in this conversation.