diff --git a/app/Http/Controllers/Admin/PriceController.php b/app/Http/Controllers/Admin/PriceController.php index 5f422a2..1d96168 100644 --- a/app/Http/Controllers/Admin/PriceController.php +++ b/app/Http/Controllers/Admin/PriceController.php @@ -14,7 +14,8 @@ use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; -use Response; +// use Response; +use Illuminate\Support\Facades\Response; class PriceController extends Controller { @@ -136,74 +137,64 @@ class PriceController extends Controller return self::whereIn('price_parkplaceid', $ids)->delete(); } - public function export(Request $request) + public function exportGet(Request $request) { $headers = [ - "Content-type" => "text/csv;charset=UTF-8", - 'Content-Encoding: UTF-8', - "Content-Disposition" => "attachment; filename=file.csv", - "Pragma" => "no-cache", - "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", - "Expires" => "0" + "Content-Type" => "text/csv; charset=UTF-8", + "Content-Disposition" => "attachment; filename=駐輪場所、料金マスタ.csv", ]; $query = Price::query(); - // 🚩 条件付きエクスポート(park_id) + // 🔹 パラメータ取得(GETでもOK) if ($request->filled('park_id')) { $query->where('park_id', $request->input('park_id')); } - // ソート if ($request->filled('sort')) { $query->orderBy($request->input('sort'), $request->input('sort_type', 'asc')); } $dataExport = $query->get(); + // 🔹 CSV列定義 $columns = [ - __('駐車場所ID'), - __('商品名'), - __('期間'), - __('駐輪場ID'), - __('駐輪場名'), - __('車種区分ID'), - __('車種区分'), - __('駐輪分類ID'), - __('駐輪分類'), - __('利用者分類ID'), - __('利用者分類'), - __('駐車車室ID'), - __('駐輪料金(税込)'), + '駐車場所ID', '商品名', '期間', '駐輪場ID', '駐輪場名', + '車種区分ID', '車種区分', '駐輪分類ID', '駐輪分類', + '利用者分類ID', '利用者分類', '駐車車室ID', '駐輪料金(税込)', ]; - $filename = "駐輪場所、料金マスタ.csv"; - $file = fopen($filename, 'w+'); + // 🔹 CSV生成 + $filename = '駐輪場所、料金マスタ.csv'; + $path = storage_path('app/' . $filename); + $file = fopen($path, 'w+'); + fwrite($file, "\xEF\xBB\xBF"); // Excel対応のBOM fputcsv($file, $columns); - foreach ($dataExport as $items) { + foreach ($dataExport as $item) { fputcsv($file, [ - $items->price_parkplaceid, - $items->prine_name, - $items->price_month, - $items->park_id, - optional($items->getPark())->park_name, - $items->psection_id, - optional($items->getPSection())->psection_subject, - $items->price_ptypeid, - optional($items->getPType())->ptype_subject, - $items->user_categoryid, - optional($items->getUserType())->print_name, - $items->pplace_id, - $items->price, + $item->price_parkplaceid, + $item->prine_name, + $item->price_month, + $item->park_id, + optional($item->getPark())->park_name, + $item->psection_id, + optional($item->getPSection())->psection_subject, + $item->price_ptypeid, + optional($item->getPType())->ptype_subject, + $item->user_categoryid, + optional($item->getUserType())->print_name, + $item->pplace_id, + $item->price, ]); } + fclose($file); - return Response::download($filename, $filename, $headers); + // 🔹 ダウンロードレスポンス + return response()->download($path, $filename, $headers)->deleteFileAfterSend(true); } - public function import(Request $request) { $file = $request->file('file'); diff --git a/app/Models/Price.php b/app/Models/Price.php index c6571db..50faa40 100644 --- a/app/Models/Price.php +++ b/app/Models/Price.php @@ -17,7 +17,7 @@ class Price extends Model 5 => '12ヶ月', ]; - protected $table = 'price_a'; + protected $table = 'price'; protected $primaryKey = 'price_parkplaceid'; protected $fillable = [ @@ -44,14 +44,14 @@ class Price extends Model { $query = self::query() ->select( - 'price_a.*', + 'price.*', \DB::raw("CONCAT_WS('/', usertype.usertype_subject1, usertype.usertype_subject2, usertype.usertype_subject3) as user_category_name"), 'psection.psection_subject', 'ptype.ptype_subject' ) - ->leftJoin('usertype', 'price_a.user_categoryid', '=', 'usertype.user_categoryid') - ->leftJoin('psection', 'price_a.psection_id', '=', 'psection.psection_id') - ->leftJoin('ptype', 'price_a.price_ptypeid', '=', 'ptype.ptype_id'); + ->leftJoin('usertype', 'price.user_categoryid', '=', 'usertype.user_categoryid') + ->leftJoin('psection', 'price.psection_id', '=', 'psection.psection_id') + ->leftJoin('ptype', 'price.price_ptypeid', '=', 'ptype.ptype_id'); // ソート対象カラム $allowedSortColumns = [ diff --git a/resources/views/admin/prices/list.blade.php b/resources/views/admin/prices/list.blade.php index b813119..127ad36 100644 --- a/resources/views/admin/prices/list.blade.php +++ b/resources/views/admin/prices/list.blade.php @@ -41,17 +41,17 @@ {{-- 削除 --}} - {{-- CSV出力(全件 or ソート条件付き) --}} -
- @csrf - - - -
+ +
+ @csrf + + + + +
- - - {{-- エクスポート(条件選択モーダル) --}} + @@ -107,7 +107,7 @@ @@ -225,65 +225,7 @@ -@endsection - - -@section('scripts') - - - - - - - + @endsection diff --git a/routes/web.php b/routes/web.php index 59e6508..62ba9f8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -184,8 +184,12 @@ Route::middleware('auth')->group(function () { Route::match(['get', 'post'], '/admin/prices/info/{id}', [PriceController::class, 'info'])->name('price_info')->where(['id' => '[0-9]+']); Route::match(['get', 'post'], '/admin/prices/delete', [PriceController::class, 'delete'])->name('prices_delete'); Route::match(['get', 'post'], '/admin/prices/import', [PriceController::class, 'import'])->name('prices_import'); + // kin 修正 - Route::post('/admin/prices/export', [PriceController::class, 'export'])->name('prices_export'); + // CSV出力(GET対応版) + Route::get('/admin/prices/export', [PriceController::class, 'exportGet']) + ->name('prices_export'); + //車種区分マスタ Route::match(['get', 'post'], '/admin/psection', [PsectionController::class, 'list'])->name('psections');