Skip to content

Instantly share code, notes, and snippets.

@Qoraiche
Last active April 18, 2021 15:42
Show Gist options
  • Save Qoraiche/f2d70ae2595217c94e835fd6b3b44ccb to your computer and use it in GitHub Desktop.
Save Qoraiche/f2d70ae2595217c94e835fd6b3b44ccb to your computer and use it in GitHub Desktop.
Laravel Excel Export Model Resource
<?php
namespace App\Exports;
use App\Http\Resources\ModelResource;
use App\Models\Model;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ModelExport implements FromArray, WithHeadings
{
use Exportable;
private $id;
/**
* ModelExport constructor.
* @param int $id
*/
public function __construct(int $id)
{
$this->id = $id;
}
public function array(): array
{
return (array)(new ModelResource(Model::find(1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment