EXPORT DATA TO EXCEL WITH LARAVEL

 EXPORT INTO EXCEL SHEET LARAVEL DATA AND TABLES

Export Data to Excel with PHP

MAKE A CONTROLLER FOR ROUTE >>

PHP ARTISAN MAKE:CONTROLLER bookingController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\booking;

class bookingController extends Controller
{
    public function booking()
    {
        $bk = booking::all();
        // dd($bk);
        return view('booking',['data'=>$bk]);
    }
}


MAKE A FUNCTION IN CONTROLLER OR MAKE A HELPERS.PHP FILE INSIDE APP FOLDER. 

INSIDE helpers.php , 
<?php
function func(){        
    $fileName = "members-data_" . date('Y-m-d') . ".xls";      
    $fields  =  array('ID',  'Car Name ',  'Driver Name',  'Customer Name',  'Mobile',  'Pickup Location',  'Drop Location',  ' Pickup Date',  'Pickup Time ',  'Adults',  'Childs',  'Message',  'Payment Mode',  'Total');        
    $excelData  =  implode("\t",  array_values($fields))  .  "\n";        
    $query  = DB::table('bookings')->get();

    foreach($query as $row){        
        $lineData  =  array($row->id,  $row->car_id,  $row->customer_id,  $row->driver_id,  $row->mobile, $row->puloc, $row->drloc, $row->pudate, $row->putime, $row->personAd, $row->personCh,$row->message,  $row->paymentMode,  $row->total);
        // array_walk($lineData,  'filterData');
        $excelData  .=  implode("\t",  array_values($lineData))  .  "\n";
    }        
    header("Content-Type:  application/vnd.ms-excel");
    header("Content-Disposition:  attachment;  filename=\"$fileName\"");    
    echo  $excelData;
}
?>

Make your helper function is accessible, Open composer.json and add after it

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/helpers.php"
       
        ]
    },

Make model to access the DB tables.

php artisan make:model booking

After that, make view blade file to access all data from file. 

Booking.blade.php
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="au theme template">
    <meta name="author" content="Hau Nguyen">
    <meta name="keywords" content="au theme template">

    <!-- Title Page-->
    <title>Bookings</title>
</head>
<!-- MAIN CONTENT-->
<div class="main-content">
    <div class="section__content section__content--p30">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <button onclick="{{func()}}" >Export To Excel</button>
                    <div class="table-responsive my-5">
                        <table class="table table-borderless table-data3">
                            <thead>
                                <tr>
                                    <th> Sr. No. </th>
                                    <th> Car Name </th>
                                    <th> Driver Name </th>
                                    <th> Customer Name </th>                                                                        
                                    <th> Mobile </th>
                                    <th> Pickup Location </th>
                                    <th> Drop Location </th>
                                    <th> Pickup Date </th>
                                    <th> Pickup Time </th>
                                    <th> Adults </th>
                                    <th> Childs </th>
                                    <th> Message </th>
                                    <th> Payment Mode </th>
                                    <th> Total </th>                                    
                                   
                                </tr>
                            </thead>
                            <tbody>
                                <?php $c = 1; ?>{{-- ['adeu' => $adfn, 'data'=>$data, 'cust'=>$cust, 'car'=>$car, 'driver'=>$driver --}}
                                @if(count($data)>0)
                                @foreach ($data as $item)
                                    <tr @if ($item->available) style="background: #ebb96e5b" @endif>
                                        <td>{{ $c++ }}</td>
                                        <td>{{ $item->car_id }}</td>
                                        <td>{{ $item->driver_id}}</td>
                                        <td>{{$item->customer_id}}</td>                                        
                                        <td>{{ $item->mobile }}</td>
                                        <td>{{ $item->puloc }}</td>
                                        <td>{{ $item->drloc }}</td>
                                        <td>{{ $item->pudate }}</td>
                                        <td>{{ $item->putime }}</td>
                                        <td>{{ $item->personAd }}</td>
                                        <td>{{ $item->personCh }}</td>
                                        <td>{{ $item->message }}</td>
                                        <td>{{ $item->paymentMode }}</td>
                                        <td>{{ $item->total }}</td>                                        
                                                                             
                                    </tr>
                                @endforeach
                                @else
                                <td colspan="15" class="text-center"> No Data Found</td>
                                @endif
                            </tbody>
                        </table>
                     
                    </div>
                   

                    <!-- END DATA TABLE -->
                </div>

            </div>


            <div class="row">
                <div class="col-md-12">
                    <div class="copyright">
                        <p>Copyright © 2022. All rights reserved. Template by <a href="https://colorlib.com">Mr</a>.
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




</body>

</html>
<!-- end document-->


Route for accessing the blade file. 

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\bookingController;


Route::get('/',[bookingController::class,'booking']);


After that booking table data will be stored in your local device. 

Run and download the data to excel format.

Enjoy coding..

   







Comments

Popular posts from this blog

[[ ROYAL CAR ]] CHANGE PASSWORD - DYNAMIC BANNER - MULTIPLE IMAGE - LOGIN LOGOUT BACK BUTTON MIDDLEWARE STOP - MAIL DIRECTLY WITH FEEDBACK WITH SAVE IN SQL DB - ADMIN REPLY EXISTING MAILS - DYNAMICALLY CSS CHANGE

Sahari

Linux Terminal