-
Action에 모델 resource id(parameter, 변수) 전달하고싶을 때Laravel/Nova 2022. 5. 19. 20:24반응형SMALL
# What?
커스텀 액션에 모델 id 직접 전달하고싶을 때 사용
# How?
* $request->resources 전달해주기(목록에서 선택한 resource id들)
@ Nova/CouponGenerator.php
(ExportCoupons::make($request->resources))->canSee(function (){ return true; })->canRun(function(){ return true; })
@ Actions/ExportCoupons.php
<?php namespace App\Nova\Actions; use App\Enums\OutgoingState; use App\Models\Coupon; use App\Models\CouponGenerator; use App\Models\OrderProduct; use App\Models\Outgoing; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Collection; use Laravel\Nova\Actions\Action; use Laravel\Nova\Fields\ActionFields; use Laravel\Nova\Fields\Text; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMapping; use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel; class ExportCoupons extends DownloadExcel implements WithMapping, FromCollection, WithHeadings { use InteractsWithQueue, Queueable; protected $couponGenerators; public function __construct($resources) { $resources = explode(",", $resources); $this->couponGenerators = CouponGenerator::whereIn("id", $resources)->get(); } public function name() { return __("ExportCoupons"); } public function collection() { $coupons = Coupon::whereIn("coupon_generator_id", $this->couponGenerators->pluck("id"))->get(); return $coupons; } public function map($model): array { $title = ""; $index = 0; foreach($this->couponGenerators as $couponGenerator){ if($index == count($this->couponGenerators) - 1){ $title.= $couponGenerator->title; break; } $index++; $title.= $couponGenerator->title.", "; } $this->withFilename("[{$title}]"." 발행쿠폰.xlsx"); $data = [ $model->title, $model->code, $model->expired_at, $model->benefit_duration, $model->benefit_price, $model->benefit_ratio ]; return $data; } public function headings(): array { $heads = [ "쿠폰명", "쿠폰코드", "만료일자", "수강기간혜택", "할인금액헤택", "할인률혜택" ]; return $heads; } }
LIST'Laravel > Nova' 카테고리의 다른 글
Order 패키지 사용법(Orderable) (0) 2022.07.02 텍스트 카드 추가하는법 (0) 2022.06.06 Lense에서 view(자세히 보기) 가리기 (0) 2022.04.16 Metrics Card 주의사항(데이터 제대로 안나올 때) (0) 2022.04.13 DateTime(날짜, 시간) 한국어로 변경하기 (0) 2022.03.19