Ask Your Question
0

What is the process of bringing in multiple rows from an excel sheet in Laravel?

asked 2021-04-15 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-12-01 20:00:00 +0000

djk gravatar image

To bring in multiple rows from an excel sheet in Laravel, you can follow these steps:

  1. Install the Maatwebsite/Laravel-Excel package using composer.
composer require maatwebsite/excel
  1. Create a new controller to handle the import process.

  2. In the controller, add the following code to import the Excel file and retrieve the data from the first sheet:

use Maatwebsite\Excel\Facades\Excel;

public function import()
{
    $data = Excel::toArray(new YourImport(), request()->file('file'));

    // Do something with the data
}

Note: Replace YourImport with the name of the class that extends the FromArray or FromCollection interface in Laravel Excel.

  1. In the view, add a file input to upload the Excel file:
<form method="POST" enctype="multipart/form-data" action="{{ route('import') }}">
    @csrf
    <input type="file" name="file">
    <button type="submit">Import</button>
</form>
  1. Finally, add a route to the import method in the controller:
Route::post('import', 'YourController@import')->name('import');
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-15 11:00:00 +0000

Seen: 9 times

Last updated: Dec 01 '22