diff --git a/beike/Shop/Http/Controllers/Controller.php b/beike/Shop/Http/Controllers/Controller.php index 1724858a..15d3e967 100644 --- a/beike/Shop/Http/Controllers/Controller.php +++ b/beike/Shop/Http/Controllers/Controller.php @@ -5,8 +5,34 @@ namespace Beike\Shop\Http\Controllers; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; +use League\Csv\CannotInsertRecord; class Controller extends \App\Http\Controllers\Controller { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; + + /** + * @param $filename + * @param $header + * @param $records + * @return mixed + * @throws CannotInsertRecord + */ + public function downloadCsv($filename, $header, $records): mixed + { + if (!str_contains($filename, '.csv')) { + $filename = $filename . '-' . date('YmdHis') . '.csv'; + } + + $headers = [ + 'Content-Type' => 'application/octet-stream', + 'Content-Transfer-Encoding' => 'binary', + 'Content-Disposition' => 'attachment; filename=' . $filename, + ]; + + $csv = \League\Csv\Writer::createFromString(''); + $csv->insertOne($header); + $csv->insertAll($records); + return response($csv, 200, $headers); + } } diff --git a/composer.json b/composer.json index 9ffafa17..05a86d22 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "laravel/horizon": "^5.10", "laravel/socialite": "^5.5", "laravel/tinker": "^2.7", + "league/csv": "^9.8", "phpoffice/phpspreadsheet": "^1.28", "spatie/laravel-permission": "^5.5", "srmklive/paypal": "^3.0", diff --git a/composer.lock b/composer.lock index ac5c65b1..d9e2d03a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "38c9df2659b7e86eb82aa7be8552c013", + "content-hash": "1c71b8ebdb3694859ad0a09bc3798d35", "packages": [ { "name": "brick/math", @@ -2463,6 +2463,96 @@ ], "time": "2022-12-11T20:36:23+00:00" }, + { + "name": "league/csv", + "version": "9.8.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/csv.git", + "reference": "9d2e0265c5d90f5dd601bc65ff717e05cec19b47" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/csv/zipball/9d2e0265c5d90f5dd601bc65ff717e05cec19b47", + "reference": "9d2e0265c5d90f5dd601bc65ff717e05cec19b47", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "ext-curl": "*", + "ext-dom": "*", + "friendsofphp/php-cs-fixer": "^v3.4.0", + "phpstan/phpstan": "^1.3.0", + "phpstan/phpstan-phpunit": "^1.0.0", + "phpstan/phpstan-strict-rules": "^1.1.0", + "phpunit/phpunit": "^9.5.11" + }, + "suggest": { + "ext-dom": "Required to use the XMLConverter and or the HTMLConverter classes", + "ext-iconv": "Needed to ease transcoding CSV using iconv stream filters" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "9.x-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "League\\Csv\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ignace Nyamagana Butera", + "email": "nyamsprod@gmail.com", + "homepage": "https://github.com/nyamsprod/", + "role": "Developer" + } + ], + "description": "CSV data manipulation made easy in PHP", + "homepage": "https://csv.thephpleague.com", + "keywords": [ + "convert", + "csv", + "export", + "filter", + "import", + "read", + "transform", + "write" + ], + "support": { + "docs": "https://csv.thephpleague.com", + "issues": "https://github.com/thephpleague/csv/issues", + "rss": "https://github.com/thephpleague/csv/releases.atom", + "source": "https://github.com/thephpleague/csv" + }, + "funding": [ + { + "url": "https://github.com/sponsors/nyamsprod", + "type": "github" + } + ], + "time": "2022-01-04T00:13:07+00:00" + }, { "name": "league/flysystem", "version": "3.15.1",