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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x