Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jamols09's avatar

DB::select() downloading Excel

I have this custom function on my Export file. I wanted to put the values of $query[] on excel but the problem is that in the documentation it only shows with Eloquent samples. How do I download the excel using db::select

use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\Exportable;


class VehicleRequestExportTrip 
{
    use Exportable;
    private $dateFrom;
    private $dateTo;
    private $driver;
    private $unit;

    public function __construct(string $dateFrom, string $dateTo, string $driver, string $unit)
    {
        $this->dateFrom = $dateFrom;
        $this->dateTo = $dateTo;
        $this->driver = $driver;
        $this->unit = $unit;
        $this->excel();
    }

    public function excel() 
    {
        $this->dateFrom = $this->dateFrom . ' 00:00:00.000';
        $this->dateTo   = $this->dateTo . ' 23:59:59.999';

        if( $this->driver == '' && $this->unit == '')
        {
            $result = DB::select("SELECT * from dispatches WHERE addedDate BETWEEN '" . $this->dateFrom . "' AND '" . $this->dateTo . "' ");
        }
        else
        {
            if( $this->driver > 0 && $this->unit > 0)
            {
                $result = DB::select("SELECT tripTicket, driver_id, type, purpose, Status, unitId FROM dispatches WHERE driver_id = '" . $this->driver . "' AND unitId = '" . $this->unit . "' AND addedDate BETWEEN '" . $this->dateFrom . "' AND '" . $this->dateTo . "' ");
            } 
        }

        $query = array();

        foreach($result as $item)
        {
            $query[] = DB::select("SELECT * FROM drivers WHERE drivers.id = ".$item->driver_id);
        }

    }
}
0 likes
10 replies
automica's avatar

@jamols09 what are you hoping this bit will do?

 $query = array();

        foreach($result as $item)
        {
            $query[] = DB::select("SELECT * FROM drivers WHERE drivers.id = ".$item->driver_id);
        }

are you aiming for a list of drivers?

jamols09's avatar

Yes. I wanted to display the list of drivers on the excel upon downloading it.

Sinnbeck's avatar

How about renaming the method from excel() to collection() and replace this

$query = array();

        foreach($result as $item)
        {
            $query[] = DB::select("SELECT * FROM drivers WHERE drivers.id = ".$item->driver_id);
        }

with this

$driverIds = $result->pluck('driver_id');
return Driver::whereIn('id', $driverIds)->get();
1 like
automica's avatar
automica
Best Answer
Level 54

@jamols09

much easier in eloquent.

<?php

class VehicleRequestExportTrip
{
    use Exportable;
    private $dateFrom;
    private $dateTo;
    private $driver;
    private $unit;

    public function __construct(string $dateFrom, string $dateTo, string $driver, string $unit)
    {
        $this->dateFrom = $dateFrom;
        $this->dateTo = $dateTo;
        $this->driver = $driver;
        $this->unit = $unit;
        $this->excel();
    }

    public function excel()
    {
        $this->dateFrom = $this->dateFrom . ' 00:00:00.000';
        $this->dateTo = $this->dateTo . ' 23:59:59.999';

        if ($this->driver == '' && $this->unit == '') {
           // $result = DB::select("SELECT * from dispatches WHERE addedDate BETWEEN '" . $this->dateFrom . "' AND '" . $this->dateTo . "' ");

            $dispatches = Dispatch::whereBetween('addedDate', [$this->dateFrom, $this->dateTo])
		->get();
        } else {

           //     $result = DB::select("SELECT tripTicket, driver_id, type, purpose, Status, unitId FROM dispatches WHERE driver_id = '" . $this->driver . "' AND unitId = '" . $this->unit . "' AND addedDate BETWEEN '" . $this->dateFrom . "' AND '" . $this->dateTo . "' ");

            $dispatches = Dispatch::whereBetween('addedDate', [$this->dateFrom, $this->dateTo])
                ->where('unitId', $this->unit)
                ->where('driver_id', $this->driver)
		->get();


        }

        $driverIds = $dispatches->pluck('driver_id');
        
        $drivers = Driver::whereIn('driver_id', $driverIds)->get();
        
    }
}
automica's avatar

@sinnbeck I think the eloquent syntax is far more elegant than just doing DB queries. Once models are defined you hardly need to do anything to get data out (which wasn't by accident).

jamols09's avatar

I am really not sure what I am missing. When exporting it gives me error such as

Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in C:\xampp7\htdocs\vms\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 65

This is how I call my export function return new VehicleRequestExportTrip($date_fr,$date_to,$driver,$unit);

automica's avatar

@jamols09 can you post what you've got now, and also the route you are using, and where you call the method.

jamols09's avatar

I have solve this by adding the ->download() and changed function excel() to collections()

1 like

Please or to participate in this conversation.