ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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

    댓글

Designed by Tistory.