Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Select Eloqeunt models, filter it and transfrom for export in xls with custom format to
// refer for this gist for first steps that are trhe same:
$parent_id = '1';
// select all children sites
$sites = Site::where('parent_id', $parent_id)->get();
$children_id = $sites->pluck('id');
// select all contacts of this children sites and also father
$contacts = Contact::whereIn('site_id', $children_id->push($parent_id))->whereDate('created', '>=', '2016-01-01')->groupBy('email')->get();
$emails = $contacts->pluck('email');
// format email and trim it
$emails->transform(function ($item, $key) {
return strtolower(str_replace(' ', '', trim($item)));
// filter for only unique emails
$emails = $emails->unique();
// filter only valid email
$filtered = $emails->filter(function ($value, $key) {
return filter_var($value, FILTER_VALIDATE_EMAIL);
// create array for export Email column with all emails
$filtered->transform(function($item, $key) {
return ['Email' => $item];
// export and download xls Excel file (here i have problems with file name)
Excel::create('your_file_name', function($excel) use ($filtered) {
$excel->sheet('your_sheet_name', function($sheet) use ($filtered) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.