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);
}
}
}
@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).
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);