Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home How to Export data in Excel and CSV

Laravel

How to Export data in Excel and CSV

JustinBy JustinAugust 30, 2022Updated:August 30, 2022No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




Hi all, In this post, i will show you how we can download data in excel/CSV. Here I will use a package, which will do the job easily. Before i am going to deep, i expect you have knowledge of Laravel 9 installations, already have database and a project. Follow the below steps.

Step 1. Package installation

$ composer require maatwebsite/excel

 

Step 2. Create Controller and Route

Create a controller using below command , If you already have controller then no need.

$ php artisan make:controller YOUR_CONTROLLER_NAME

Create a function for export inside your controller

    public function export_country(Request $req){
        $adv_id=$req->adv_id;
        $campaign_id=$req->campaign_id;
        return Excel::download(new AdvCountryExport($adv_id, $campaign_id), 'CountryReport.xlsx');
    }

Now write in Route file like below.

Route::POST("/export_country",[AdvertiserReportController::class,'export_country']);

Step 3. Create Export Class

Use the below command

php artisan make:export AdvCountryExport --model=AdvertiserReportModel

Now a new class is created inside App/Exports/AdvCountryExport

This will look something like below code.

<?php

namespace App\Exports;

use App\Models\AdvertiserReportModel;
use Maatwebsite\Excel\Concerns\FromCollection;

class AdvCountryExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return AdvertiserReportModel::all();
    }
}

Now change according to our need.

<?php

namespace App\Exports;

use Illuminate\Support\Facades\DB;
use App\Models\AdvertiserReportModel;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class AdvCountryExport implements FromCollection,WithHeadings
{
    protected $adv_id, $campaign_id;

    function __construct($adv_id, $campaign_id) {
            $this->adv_id = $adv_id;
            $this->campaign_id = $campaign_id;
    }
    /**
    * @return \Illuminate\Support\Collection
    */
    public function headings(): array
    {
        return [
            'Date',
            'Country',
            'Impressions',
            'Clicks',
            'CTR (%)',
            'Spent',
        ];
    }
    public function collection()
    {
        return collect(DB::select(DB::raw("SELECT sac_date, sac_country, SUM(sac_impressions) as imp, SUM(sac_clicks) as clicks, (SUM(sac_clicks)/SUM(sac_impressions))*100 ,SUM(sac_spent) as spent FROM `stats_adv_country` WHERE sac_adv_id=$this->adv_id AND sac_campaign_id=$this->campaign_id GROUP BY sac_country, sac_date ORDER BY sac_date DESC;")));
    }
}

Now run code when calling you routes you will get a file. Like below image

Download excel

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

download excel download excel laravel download excel laravel 9
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d