mhmaarafath's avatar

Laravel Excel - live preview of imported row

Package - https://github.com/Maatwebsite/Laravel-Excel

I need to add imported data to database row by row not all record at once.

So, I could display newly added row on frontend

0 likes
3 replies
mhmaarafath's avatar

StudentImport.php

public function model(array $row)
{
    $student = new Student([
        'student_code' => $row['student_code'],
        'name' => $row['name'],
        'gender' => $row['gender'],
        'upload_batch_id' => Session::get('upload_batch_id'),
        'school_id' => '1',
    ]);
    $student->save();
}

UploadTable.php (Livewire)

public function uploadData()
{
    $this->validate();

    $file_name = $this->upload->getClientOriginalName();
    $data = UploadBatch::create([
        'user_id' => auth()->user()->id,
        'file_name' => $file_name
    ]);
    Session::put('upload_batch_id', $data->id);
    Excel::import(new StudentImport, $this->upload);
}


public function render()
{
    return view('livewire.upload-table',[
        'uploads' => Student::all()
    ]);
}

upload-table.bade.php

    <table>
        <tbody wire:poll.750ms>
        @foreach($uploads as $upload)
            <tr>
                <td>{{$upload->name}}</td>
                <td>{{$upload->school->name}}</td>
            </tr>
        @endforeach
        </tbody>
    </table>

What i want is to render real time uploaded data. Rather than showing all uploaded record at once.

Please or to participate in this conversation.