Search code examples
mysqllaraveleloquentlaravel-nova

Laravel hydrate not using all fields


I have two tables, operations and upload_operations. Every day I'm truncating and importing a fresh set of data into upload_operations, and then running some Actions in Nova to compare, update, and add new data from upload_operations into operations. One of my actions is not working as expected:

public function handle()
    {
        $new_uploads = DB::select('
            SELECT
            *
            FROM
            upload_operations u
            LEFT JOIN operations o USING (operation_id)
            WHERE
            o.operation_id IS NULL');
        $new_operations = UploadOperation::hydrate($new_uploads);

        Log::info('New operations count: ' . $new_operations->count());

        foreach ($new_operations->chunk(25) as $chunk) {
            foreach($chunk as $no) {
                Operation::create([
                    'certifier_name' => $no->certifier_name,
                    'certifier_website' => $no->certifier_website,
                    'certifier_email_address' => $no->certifier_email_address,
                    'operation_id' => $no->operation_id,
                    'operation_name' => $no->operation_name,
                    ...
                    // rest of rows
                    ...
                ]);
            }
        }

        return Action::message('New uploads added!');
    }

The SQL statement works. With my current data it returns 63 rows and every column is populated with data. However, when I run the action my database has 63 records where all columns except for the operation_id (the primary key) are blank.

Am I misusing hydrate()? Is there a better way I could be inserting these records into the operations table?


Solution

  • Even though the SQL works in TablePlus, Laravel was not selecting all columns. I fixed it by having to explicitly select every column in the SQL.