John Davidson

php - get data after save excel file with `maatwebsite/excel`

0 comments
Message:


Project in Laravel (9), and PHP (8.1).


I want to import an excel file and use maatwebsite/excel (3.1) package.


I can import a file, and save the file into the model, like this:


import class:


class BankTransfersHistoryImport implements ToModel, WithHeadingRow, WithValidation
{
use Importable;

/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new BankTransfersHistory([
'loanId' => $row['loanId'],
'actionDate' => transformDate($row['actionDate']),
'worth' => $row['worth'],
.
.
]);
}

public function headingRow(): int
{
return 2;
}

public function rules(): array
{
return [
'*.loanId' => ['required', 'numeric'],
... some roles ...
];
}
}

controller:


        $import = new BankTransfersHistoryImport;

try {
// date validation
$collection = $import->toCollection($file);
... some validation about the date ...

$import->import($file);
... check and update rows ...

return [
"message" => some message,
"data" => [
some data
],
];

} catch (\Maatwebsite\Excel\Validators\ValidationException$e) {
$failures = $e->failures();

foreach ($failures as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
return $failures;
}

My question is:


If I can get the response of the file after saving the data, and that will give me the data with the id of the row that was saved.


In some cases, I will have to update a row. That's why I would like to get the ID.


Now in the check and update rows section, I update by loanId + actionDate. I want it to be done by ID.




something like this:


code:
$data = $import->import($file);


data will be like:


[
{
"id": 1,
"loanId": 21001,
"actionDate": "2020-01-02T00:00:00.000000Z",
"worth": 2997.09,
"offerId": 1,
},
{
"id": 2,
"loanId": 21002,
"actionDate": "2020-01-02T00:00:00.000000Z",
"worth": 3000,
"offerId": 10,
},
]


My Answer:
$row['loanId'],
'amount' => $row['amount'],
'date' => $row['date'],
'description' => $row['description'],
]);
}
}

controller method:

public function import(Request $request)
{
$file = $request->file('file');

Excel::import(new BankTransfersHistoryImport, $file);

return redirect()->back()->with('success', 'File imported successfully');
}

Now, after importing the excel file and saving the data into the model, you can retrieve the data from the database using Eloquent ORM. For example, you can retrieve all the bank transfer history records like this:

$bankTransfers = BankTransfersHistory::all();

foreach ($bankTransfers as $bankTransfer) {
echo $bankTransfer->loanId;
echo $bankTransfer->amount;
echo $bankTransfer->date;
echo $bankTransfer->description;
}

This code will fetch all the bank transfer history records from the database and display the loan ID, amount, date, and description for each record. You can customize the retrieval logic based on your requirements.

Rate this post

4 of 5 based on 9371 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us