Export by selectedRowsQuery #260
Replies: 1 comment 1 reply
-
This is not an issue with the package, but I'll give you an example from one of my projects anyway: public function exportSelected()
{
if ($this->selectedRowsQuery->count() > 0) {
return (new UserExport($this->selectedRowsQuery))->download($this->tableName.'.xlsx');
}
$this->notify(__('You did not select any users to export.'), 'danger');
} UserExport.php <?php
namespace App\Domains\User\Exports;
use Illuminate\Database\Eloquent\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
/**
* Class UserExport
*
* @package App\Domains\User\Exports
*/
class UserExport implements FromQuery, WithMapping, WithHeadings
{
use Exportable;
/**
* @var Builder
*/
public Builder $builder;
/**
* UserExport constructor.
*
* @param Builder $builder
*/
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* @return Builder|\Illuminate\Database\Query\Builder
*/
public function query()
{
return $this->builder;
}
/**
* @return string[]
*/
public function headings(): array
{
return [
'Name',
'E-mail',
'User Type',
'Active',
];
}
/**
* @param mixed $row
*
* @return array
*/
public function map($row): array
{
return [
$row->name,
$row->email,
ucfirst($row->type),
$row->active === true ? 'Yes' : 'No',
];
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
if ($this->selectedRowsQuery->count() > 0) {
return (new UserExport($this->selectedRowsQuery))->download($this->tableName.'.xlsx');
}
in my UserExport like this below
namespace App\Exports;
use App\Models\UserExport;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class UserExport implements FromQuery, Responsable
{
use Exportable;
protected $id;
public function __construct($id)
{
$this->id = $id;
}
}
then it return
SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s) (SQL: select * from
users
whereid
in (select * fromusers
wherestatus
= 1 andid
in (15) order bynama
asc) order byusers
.id
asc limit 1000 offset 0)and it's not exported...
any suggestion please?
Beta Was this translation helpful? Give feedback.
All reactions