PHP code example of alfonsobries / laravel-spreadsheet-importer

1. Go to this page and download the library: Download alfonsobries/laravel-spreadsheet-importer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alfonsobries / laravel-spreadsheet-importer example snippets




namespace App\Models;

use Alfonsobries\LaravelSpreadsheetImporter\Traits\InteractsWithImporter;
use Alfonsobries\LaravelSpreadsheetImporter\Contracts\Importable;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model implements Importable
{
    use InteractsWithImporter;

    /**
     * Return the full path of the file that will be imported
     * @return string
     */
    public function getFileToImportPath() {
        // Notice that this line should be adapted to your application, this is an example for
        // a path that comes from a file that was stored using the spatie media library package
        return $this->getFirstMedia('file')->getPath();
    }

    /**
     * Return the temporaly table name that will be used to store the spreadsheet contents
     *
     * @return string
     */
    public function getTemporalTableName() {
        // This is an example you should adapt this line to your own application
        // You should create a method that always return the same value for the same model
        return sprintf('file_%s', $this->id);
    }

$totalSales = $myModel->tempData()->sum('sales');
bash
php artisan vendor:publish --provider="Alfonsobries\LaravelSpreadsheetImporter\LaravelSpreadsheetImporterServiceProvider" --tag="config"
 php 
public function store(Request $request, MyModel $model)
{
    $model->addFile($request->file);

    \Alfonsobries\LaravelSpreadsheetImporter\Jobs\StartImport::dispatch($model);

    // ...The rest of the code
}