1

So I want to send a email in a particular time, when my command runs and with that email, i want to send an excel file with that email,i also have some data which i want to append in the excel file, i have used Excel::download() method but it creates the excel file in this path "storage/framework/laravel-excel",with the name like "laravel-excel-9ssyjrYnrNNOBU2wfi5eaGtXsRySkj4X" which i don't want, i want to store that excel file somewhere else in the storage folder and then retrieve it and send it with the email.

As of Now I have deleted all the files from the path "storage/framework/laravel-excel/" first and then created the new excel file which will store in the path which is mentioned in previous line , and then fetch the new file with the email and send it. But this approach is not looking good or code reviewer will definitely dissappointed by this code. Is there any better solution to this , i already tried store method but it doesn't give me result

Public function build(){

        $directoryPath = storage_path('framework/laravel-excel/');

        $files = File::glob("{$directoryPath}*.xlsx");
        if($files){
            File::delete($files);
        }
       
        Excel::download(new SellerPayoutExport([$this->record]) , 'SellerPayout'. '.'. 'xlsx');
        $latestFile = reset($files);
    
        return $this->from(core()->getSenderEmailDetails()['email'], core()>getSenderEmailDetails()  ['name'])
            ->to('[email protected]')
            ->subject(trans('customshop::app.mail.seller-payout.subject'))
            ->view('customshop::emails.sales.seller-payouts')
            ->attach($latestFile, [
                'as' => 'seller_payouts.xlsx',
                'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            ]
        );  
}

1 Answer 1

1

When you want to customize the storage location of the generate file, You can use the store method provided by Laravel Excel to save the file to a specific location Instead of deleting files from the default path.

public function build()
{
    // Define your custom path here
    $customPath = storage_path('app/excel-files/');

    // Make sure the directory exists
    if (!File::exists($customPath)) {
        File::makeDirectory($customPath, 0755, true, true);
    }

    // Generate and store the Excel file
    $excelFileName = 'SellerPayout.xlsx';
    $excelFilePath = $customPath . $excelFileName;
    
    Excel::store(new SellerPayoutExport([$this->record]), $excelFileName, 'local');

    return $this->from(core()->getSenderEmailDetails()['email'], core()->getSenderEmailDetails()['name'])
        ->to('[email protected]')
        ->subject(trans('customshop::app.mail.seller-payout.subject'))
        ->view('customshop::emails.sales.seller-payouts')
        ->attach($excelFilePath, [
            'as'   => $excelFileName,
            'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        ]);
}

This way, you won't have to delete files from the default path, and the generated Excel file will be stored in your desired location. Adjust the $customPath as needed for your application's structure.

I'm not familiar with the specifics of your application, but if it happens to interact with various storage systems, you can use Storage::disk() in your code. In the above example, I've configured the default storage disk to be 'local'.

cheers

Sign up to request clarification or add additional context in comments.

3 Comments

thanks buddy but it didn't work i use the same store method but with little change here is the code , public function build() { Excel::store(new SellerPayoutExport($this->record) , '/excel/Payout'. '.'. 'xlsx'); $attachmentFile = "storage/app/public/excel/Payout.xlsx"; return // Rest of the code
Is there a log available to review errors and warnings?
it only shows unable to open file "file path" in the terminal and some error in log file, and didn't even create the file , but i finally solve this error. Thanks for the resonse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.