Journal Entry, September 20th to September 27th 2019

September 27, 2019 · 1 min read

Laravel Nova

  1. Fri Sep 27

    • Customize download file name using record information.

      /**
       * Get the actions available for the resource.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function actions(Request $request)
      {
          $fileName = $this->getDownloadFileName($request);
          return [
              (new DownloadExcel)->withFilename($fileName)
                                  ->withWriterType(\Maatwebsite\Excel\Excel::CSV)
                                  ->withName('Download CSV')
                                  ->withHeadings()
                                  ->only('code')
                                  ->onlyOnIndex()
                                  ->withoutConfirmation(),
          ];
      }
      
      /**
       * Get the filename for the DownloadExcel action.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return string
       */
      private function getDownloadFileName(Request $request)
      {
          $result = 'codes-' . time() . '.csv';
          try {
              $batch = $request->findModelQuery()->getParent();
              $result = \Str::slug($batch->codeType->name) . "-{$batch->id}-codes.csv";
          } catch (\Exception $exception) {
          }
          return $result;
      }