Laravel-excel: [QUESTION] How to Export Model with Relationship

Created on 4 Jul 2019  Â·  6Comments  Â·  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.1.3
  • Laravel version: 5.8.*
  • Package version: 3.1

Description

I want export relationship tables.
Here is the relationship:

  • Person has many Education
  • Person has many Work Experience.

I ran to package documentation, unable to find how to export relationship tables.
Here is the code,

class ReportExport implements FromCollection, WithHeadings, ShouldAutoSize
{
    private $people;

    public function __construct($people)
    {
        $this->people = $this->formatEloquentCollection($people);
    }
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return  $this->people;
    }

    private function formatEloquentCollection($collection)
    {
        return  collect($collection->toArray())->map(function ($i, $k) {
            unset($i['id']);
            return  ['No' => $k + 1] + $i;
        });
    }

    public function headings(): array
    {

        $columns = [];
        if (count($this->people)) {
            foreach ($this->people->first()  as $attribute => $value) {
                array_push($columns, array_reduce(preg_split('/(?=[A-Z])/', $attribute), function ($p, $n) {
                    return  ucfirst($p) . ' ' .  ucfirst($n);
                }));
            }
        }

        return $columns;
    }
}

Additional Information

The $people that passed in constructor is eager loaded collection and that have a relations.
Here is dd() output of $people.

Collection {#794 â–¼
  #items: array:109 [â–¼
    0 => Person {#813 â–¼
      #fillable: array:18 [ …18]
      #connection: "mysql"
      #table: "people"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:23 [ …23]
      #original: array:23 [ …23]
      #changes: []
      #casts: []
      #dates: array:1 [ …1]
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [ …1]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [ …1]
      +mediaConversions: []
      +mediaCollections: []
      #deletePreservingMedia: false
      #unAttachedMediaLibraryItems: []
      #forceDeleting: false
    }
    1 => Person {#814 â–¶}
    2 => Person {#815 â–¶}
    3 => Person {#816 â–¶}
    4 => Person {#817 â–¶}
    5 => Person {#818 â–¶}
    6 => Person {#819 â–¶}
    7 => Person {#820 â–¶}
    8 => Person {#821 â–¶}
    9 => Person {#822 â–¶}
    10 => Person {#823 â–¶}
    11 => Person {#824 â–¶}
    12 => Person {#825 â–¶}
    13 => Person {#826 â–¶}
    14 => Person {#827 â–¶}
    15 => Person {#828 â–¶}
    16 => Person {#829 â–¶}
    17 => Person {#830 â–¶}
    18 => Person {#831 â–¶}
    19 => Person {#832 â–¶}
    20 => Person {#833 â–¶}
    21 => Person {#834 â–¶}
    22 => Person {#835 â–¶}
    23 => Person {#836 â–¶}
    24 => Person {#837 â–¶}
    25 => Person {#838 â–¶}
    26 => Person {#839 â–¶}
    27 => Person {#840 â–¶}
    28 => Person {#841 â–¶}
    29 => Person {#842 â–¶}
    30 => Person {#843 â–¶}
    31 => Person {#844 â–¶}
    32 => Person {#845 â–¶}
    33 => Person {#846 â–¶}
    34 => Person {#847 â–¶}
    35 => Person {#848 â–¶}
    36 => Person {#849 â–¶}
    37 => Person {#850 â–¶}
    38 => Person {#851 â–¶}
    39 => Person {#852 â–¶}
    40 => Person {#853 â–¶}
    41 => Person {#854 â–¶}
    42 => Person {#855 â–¶}
    43 => Person {#856 â–¶}
    44 => Person {#857 â–¶}
    45 => Person {#858 â–¶}
    46 => Person {#859 â–¶}
    47 => Person {#860 â–¶}
    48 => Person {#861 â–¶}
    49 => Person {#862 â–¶}
    50 => Person {#863 â–¶}
    51 => Person {#864 â–¶}
    52 => Person {#865 â–¶}
    53 => Person {#866 â–¶}
    54 => Person {#867 â–¶}
    55 => Person {#868 â–¶}
    56 => Person {#869 â–¶}
    57 => Person {#870 â–¶}
    58 => Person {#871 â–¶}
    59 => Person {#872 â–¶}
    60 => Person {#873 â–¶}
    61 => Person {#874 â–¶}
    62 => Person {#875 â–¶}
    63 => Person {#876 â–¶}
    64 => Person {#877 â–¶}
    65 => Person {#878 â–¶}
    66 => Person {#879 â–¶}
    67 => Person {#880 â–¶}
    68 => Person {#881 â–¶}
    69 => Person {#882 â–¶}
    70 => Person {#883 â–¶}
    71 => Person {#884 â–¶}
    72 => Person {#885 â–¶}
    73 => Person {#886 â–¶}
    74 => Person {#887 â–¶}
    75 => Person {#888 â–¶}
    76 => Person {#889 â–¶}
    77 => Person {#890 â–¶}
    78 => Person {#891 …31}
    79 => Person {#892 …31}
    80 => Person {#893 …31}
    81 => Person {#894 …31}
    82 => Person {#895 …31}
    83 => Person {#896 …31}
    84 => Person {#897 …31}
    85 => Person {#898 …31}
    86 => Person {#899 …31}
    87 => Person {#900 …31}
    88 => Person {#901 …31}
    89 => Person {#902 …31}
    90 => Person {#903 …31}
    91 => Person {#904 …31}
    92 => Person {#905 …31}
    93 => Person {#906 …31}
    94 => Person {#907 …31}
    95 => Person {#908 …31}
    96 => Person {#909 …31}
    97 => Person {#910 …31}
    98 => Person {#911 …31}
    99 => Person {#912 …31}
    100 => Person {#913 …31}
    101 => Person {#914 …31}
    102 => Person {#915 …31}
    103 => Person {#916 …31}
    104 => Person {#917 …31}
    105 => Person {#918 …31}
    106 => Person {#919 …31}
    107 => Person {#920 …31}
    108 => Person {#921 …31}
  ]
}
question

Most helpful comment

Based on your description I don't really understand what you are trying to accomplish.

These are the two options we have accounted for:

You can access all relationship values like you would with Eloquent:

public function map($model): array
    {
        return [
            $model->relation->attribute
        ];
    }

You can also return multiple rows:

public function map($model): array
    {
        $rows = [];
         foreach($model->relation as $relation)
        { 
               $rows[]= [
                    $relation->attribute,
              ];
        }
       return $rows;
    }

All 6 comments

Did you have a look at Mapping rows yet?

Did you have a look at Mapping rows yet?

Thanks for the reply. I saw it, but I'm a fresh programmer, It's not clear enough for me. So could you give me an example?
Here is the relationship schema

People:

 class CreatePeopleTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('people', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('firstName', 35);
            $table->string('middleName', 35);
            $table->string('lastName', 35);
            $table->integer('gender');
            $table->string('kebeleId')->unique();
            $table->date('birthday');
            $table->string('birthPlace', 255);
            $table->string('ethnicGroup', 255);
            $table->string('citizenship', 255);
            $table->integer('maritalStatus');
            $table->integer('religion');
            $table->bigInteger('home')->nullable();
            $table->bigInteger('mobile')->unique();
            $table->string('email', 255)->unique();
            $table->string('town', 70);
            $table->string('kebele', 255);

            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onUpdate('cascade');
            $table->unsignedBigInteger('zone_id');
            $table->foreign('zone_id')->references('id')->on('zones')->onUpdate('cascade');
            $table->unsignedBigInteger('department_id')->nullable();
            $table->foreign('department_id')->references('id')->on('departments')->onUpdate('cascade');

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists('people');
    }
}

EducationBackgrounds:

class CreateEducationBackgroundsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('education_backgrounds', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->integer('levelOfEducation');
            $table->string('nameOfInstitution');
            $table->date('yearOfGraduate');
            $table->string('certificateId', '255')->unique();

            $table->date('certifiedDate');

            $table->unsignedBigInteger('field_id')->nullable();
            $table->foreign('field_id')
                ->references('id')
                ->on('fields')->onUpdate('cascade');
            $table->unsignedBigInteger('person_id');
            $table->foreign('person_id')
                ->references('id')->on('people')
                ->onUpdate('cascade');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onUpdate('cascade');

            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists('education_backgrounds');
    }
}

Based on your description I don't really understand what you are trying to accomplish.

These are the two options we have accounted for:

You can access all relationship values like you would with Eloquent:

public function map($model): array
    {
        return [
            $model->relation->attribute
        ];
    }

You can also return multiple rows:

public function map($model): array
    {
        $rows = [];
         foreach($model->relation as $relation)
        { 
               $rows[]= [
                    $relation->attribute,
              ];
        }
       return $rows;
    }

Thanks, That's works.

@gech2me I has similar issue could you help me, I used @patrickbrouwers example it worked but as it will be one column of arrays, so how can I covert array values into excel columns?

        public function map($list): array
        {
            $rows = [];
            foreach ($list->list_products as $listP) {

                array_push($rows,[
                    $list->id,
                    $list->name,
                    $list->customer->social_razon,
                    $list->customer->seller->name,
                    $listP->product->model,
                    Carbon::parse($list->created_at)->toFormattedDateString(),
                    Carbon::parse($list->updated_at)->toFormattedDateString(),
                    ]);
            }

            return $rows;
        }


 Id    Name list Customer   Seller  Product Model      Created  updated
    18  kris local       tip     Mario  sin           Oct 14, 2020  Oct 15, 2020
    18  kris local       tip     Mario  coca              Oct 14, 2020  Oct 15, 2020
    17  Super List   T.I.  Mario    sin           Oct 14, 2020  Oct 15, 2020
    17  Super List   T.I.   Mario   2030              Oct 14, 2020  Oct 15, 2020
    17  Super List   T.I.   Mario   coca              Oct 14, 2020  Oct 15, 2020
    17  Super List   T.I.   Mario   coca              Oct 14, 2020  Oct 15, 2020
    17  Super List   T.I.   Mario   model 3           Oct 14, 2020  Oct 15, 2020
Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhghazaliakbar picture muhghazaliakbar  Â·  3Comments

amine8ghandi8amine picture amine8ghandi8amine  Â·  3Comments

kurianic picture kurianic  Â·  3Comments

matthewslouismarie picture matthewslouismarie  Â·  3Comments

pamekar picture pamekar  Â·  3Comments