So i have this code:
PengajuanDetailController.php
public function import(Request $request){
$file = $request->file('file');
$namaFile = $file->getClientOriginalName();
$file->move('DataDetailPengajuan', $namaFile);
Excel::import(new SubmissionDetailImport, public_path('/DataDetailPengajuan/'.$namaFile));
return redirect()->back()->with("status", ["status" => true, "message" => "Success importing detail"]);
}
SubmissionDetailImport.php
<?php
namespace App\Imports;
use App\SubmissionDetail;
use Maatwebsite\Excel\Concerns\ToModel;
class SubmissionDetailImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new SubmissionDetail([
'submission_id' => $row[1],
'nama_barang' => $row[2],
'image_path' => $row[3],
'jumlah' => $row[4],
'harga_satuan' => $row[5],
'harga_total' => $row[6],
'keterangan' => $row[7],
]);
}
}
I wanted to make the imported row value of 'submission_id' equal to an id,
for example: http://127.0.0.1:8000/pengajuan/4/
the row value of 'submission_id' becomes 4 of the "/4/" instead of the original imported value, how to do?
My Answer:
You can achieve this by modifying the `model` method in your `SubmissionDetailImport` class. You can fetch the submission id from the URL and use it as the value for the `submission_id` field in the imported row.
Here's how you can do it:
php
public function model(array $row)
{
$submissionId = request()->segment(2); // Get the submission id from the URL
return new SubmissionDetail([
'submission_id' => $submissionId,
'nama_barang' => $row[2],
'image_path' => $row[3],
'jumlah' => $row[4],
'harga_satuan' => $row[5],
'harga_total' => $row[6],
'keterangan' => $row[7],
]);
}
In this code snippet, we are fetching the submission id from the URL using `request()->segment(2)`. This will get the second segment of the URL, which in your case should be the submission id. You can adjust the segment number based on your URL structure.
By using this approach, the `submission_id` field in the imported row will be set to the submission id from the URL instead of the original imported value.
Rate this post
3 of 5 based on 6970 votesComments