Xerakon
Xerakon

Reputation: 159

Laravel Unable to copy contents of one database into another

I'm not sure how to go about this. I have a working table that I manipulate data on to match the rest of our database, and a published table that I want to transfer the finalized data onto. Although truncate() performs and clears out the Report table, the Prescription table remains empty after this runs. Is this some sort of Eloquent issue that I'm not understanding?

Here is my code:

// call working table
$reports = Report::orderBy('id', 'asc');

// loop through every working report and save as a new prescription
foreach ($reports as $report) {
    $prescription = new Prescription();
    $prescription->script_id = $report->script_id;
    $prescription->medication = $report->medication;
    $prescription->fill_date = $report->fill_date;
    $prescription->type = $report->type;
    $prescription->quantity = $report->quantity;
    $prescription->supply = $report->supply;
    $prescription->insurance = $report->insurance;
    $prescription->rx_number = $report->rx_number;
    $prescription->copay = $report->copay;
    $prescription->tr = $report->tr;
    $prescription->cogs = $report->cogs;
    $prescription->fees = $report->fees;
    $prescription->ncr = $report->ncr;
    $prescription->save();
}

// truncate working table
$reports->truncate();

// return
$message = 'All records transferred to prescriptions table.';
return redirect('reports')->with('success', $message);

Upvotes: 0

Views: 34

Answers (1)

aynber
aynber

Reputation: 23011

You're missing the ->get() on your Report query, to actually retrieve the results. As it is, $reports only contains the query builder object.

$reports = Report::orderBy('id', 'asc')->get();

You'll most likely need to adjust the truncate call afterwards:

Report::truncate();

Upvotes: 3

Related Questions